The get_field()
function can be used in Form & Actions hooks to retrieve the fields input values entered by the user during the form submission. In order to retrieve the field value from the database you must set a valid post ID as reference.
Following that logic, it is also possible to use others ACF functions such as have_rows()
and get_sub_field()
to interact with fields input or database values.
function | Output |
get_field('my_field') | “my_field” Input value |
get_field('my_field', false, false) | “my_field” Input value (unformatted) |
get_field('my_field', 128) | “my_field” DB value |
get_field('my_field', 128, false) | “my_field” DB value (unformatted) |
The acfe_form_get_action()
function can be used in form & actions hook to retrieve a previous action output. This function is useful to use a previous action data within the current action.
/*
* Get Action Output
*
* @param string $action Action type or name
* @param string $key Retrieve a specific key from the returned array
* @return mixed
*/
acfe_form_get_action([$action = ''], [$key = ''])
acfe_form_get_actions()
function | Output |
acfe_form_get_action() | Get the latest action output |
acfe_form_get_actions() | Get all actions output |
acfe_form_get_action('email') | Get the latest “E-mail” action output |
acfe_form_get_action('post') | Get the latest “Post” action output |
acfe_form_get_action('term') | Get the latest “Term” action output |
acfe_form_get_action('user') | Get the latest “User” action output |
acfe_form_get_action('my-action') | Get the action named “my-action” output |
acfe_form_get_action('post', 'ID') | Get the “ID” key from the latest “Post” action output |
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 or in a multi-step setup. Usage example:
/*
* Is Form Success
*
* @param string $form Form name
* @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_form_get_action('post', 'ID');
// Retrieve the title and permalink
$title = get_the_title($post_id);
$permalink = get_permalink($post_id);
// Print info
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.