By default, ACF values functions are wrapped within the form context. This allows to use functions such as get_field()
, get_fields()
, have_rows()
, get_sub_field()
without any $post_id
to retrieve the field input values.
add_action('acfe/form/submit_form/form=my-form', 'my_form_submission');
function my_form_submission($form){
$inputs = get_fields(); // all inputs values
$inputs = get_fields(false, false); // all inputs values (unformatted)
$my_field = get_field('my_field'); // input value
$my_field = get_field('my_field', false, false); // input value (unformatted)
if(have_rows('my_repeater')):
while(have_rows('my_repeater')): the_row();
$sub_field = get_sub_field('sub_field'); // subfield input value
$sub_field = get_sub_field('sub_field', false); // subfield input value (unformatted)
endwhile;
endif;
}
To retrieve values from the database, you’ll have to pass the corresponding $post_id
. Usage example:
add_action('acfe/form/submit_form/form=my-form', 'my_form_submission');
function my_form_submission($form){
$inputs = get_fields(45); // all db values
$inputs = get_fields(45, false); // all db values (unformatted)
$my_field = get_field('my_field', 45); // db value
$my_field = get_field('my_field', 45, false); // db value (unformatted)
// repeater
if(have_rows('my_repeater', 45)):
while(have_rows('my_repeater', 45)): the_row();
$sub_field = get_sub_field('sub_field'); // subfield db value
$sub_field = get_sub_field('sub_field', false); // subfield db value (unformatted)
endwhile;
endif;
}
The acfe_get_form_action()
function can be used to retrieve a previous action output. This function is useful to get a previous action data.
/**
* Get Action Output
*
* @param string $path Action name. Allows dot notation to get sub value
* @param string $default The default returned value, if none was found
* @return mixed
*/
acfe_get_form_action($path = '', [$default = null])
/**
* Get All Actions Output
*
* @return array
*/
acfe_get_form_actions()
Considering the following form configuration, which create a page and then send an email:
acfe_form(array(
'name' => 'my-form',
'field_groups' => array(
'group_5ffd27b7d0c62',
),
'actions' => array(
// create post
array(
'action' => 'post',
'name' => 'my-post',
'type' => 'insert_post',
'save' => array(
'post_type' => 'page',
'post_status' => 'publish',
'post_title' => '{field:field_5ff7ee7cdd8f3}',
),
),
// send email
array(
'action' => 'email',
'name' => 'my-email',
'email' => array(
'from' => '[email protected]',
'to' => '[email protected]',
'subject' => 'Subject',
'content' => '{fields}',
),
),
),
));
We can hook in the Email filter acfe/form/submit_email_args
in order to retreive the previous Post Action output, and use the “Post Title” as “Subject”. Usage example:
add_filter('acfe/form/submit_email_args/form=my-form', 'my_email_args', 10, 3);
function my_email_args($args, $form, $action){
// get the whole post output
$post = acfe_get_form_action('post');
/**
* $post = array(
* 'ID' => 1554,
* 'post_author' => 1,
* 'post_date' => '2020-08-19 20:53:31',
* 'post_date_gmt' => '0000-00-00 00:00:00',
* 'post_content' => '',
* 'post_title' => 'My Post',
* ...
* )
*/
// assign subject
// use previous post action 'post_title' output
$args['subject'] = acfe_get_form_action('post.post_title');
// return
return $args;
}
Since the Post Action is named my-post
, we can also specifically target this action using acfe_get_form_action('my-post.post_title')
.
The acfe_is_form_success()
can be used to check if the current page is a Form Success Page. This helper can be useful to display a custom success message . Usage example:
/*
* Is Form Success
*
* @param string $form Form name or form array
* @return bool
*/
acfe_is_form_success([$form = ''])
// success Message
if(acfe_is_form_success('my-form')){
echo '<h3>Thank you</h3>';
echo '<p>The form has been successfully submitted.</p>';
// retrieve the ID of the created/updated post in a Post Action
$post_id = acfe_get_form_action('post.ID');
// retrieve the title and permalink
$title = get_the_title($post_id);
$permalink = get_permalink($post_id);
// print message
echo '<p><a href="' . $permalink . '">' . $title . '</a> has been created</p>';
// render Form
}else{
acfe_form(array(
'name' => 'my-form'
));
}
Using that helper, we can setup multiple forms acting like steps on the same page. Usage example:
// no form was submitted
if(!acfe_is_form_success()){
// display form-1
acfe_form(array(
'name' => 'my-form-1'
));
// form-1 was submitted
}elseif(acfe_is_form_success('my-form-1')){
// display form-2
acfe_form(array(
'name' => 'my-form-2'
));
// form-2 was submitted
}elseif(acfe_is_form_success('my-form-2')){
// display form-3
acfe_form(array(
'name' => 'my-form-3'
));
}
It is possible to import exported Json form using the acfe_import_form()
function. Usage example:
add_action('admin_init', 'my_acf_import_form');
function my_acf_import_form(){
// retrieve json file
$file = get_stylesheet_directory() . '/acfe-form-export.json';
$content = file_get_contents($file);
// import form
acfe_import_form($content);
}
You only need to run that hook one time, so once the form is imported you can remove that code.