The Custom Action is a canvas for developers that want to trigger a custom script on form submission. Out-of-the-box, this action will do nothing on form submission.
Setting name | Description |
Action name | Alias name allowing to use targeted hook. It cannot be named custom , email , post or term . |
By default, there is no validation for this Action. You can use the acf/validate_value
hook to validate each fields individually.
To validate the whole form, you can use the acfe/form/validation/my-action
hook and throw errors with the acfe_add_validation_error()
function. Usage example:
/*
* Custom Validation
*
* @array $form Form settings
* @string $post_id Post ID (where the form is displayed)
* @string $action Action name
*/
action('acfe/form/validation/my-action', $form, $post_id, $action);
action('acfe/form/validation/my-action/form=my-form', $form, $post_id, $action);
add_action('acfe/form/validation/my-action', 'my_form_validation', 10, 3);
function my_form_validation($form, $post_id, $action){
// Get field input value
$my_field = get_field('my_field');
if($my_field === 'Company'){
// Add validation error
acfe_add_validation_error('my_field', 'The value Company is not allowed');
}
}
The preparation phase is triggered after the form validation and before its submission. The acfe/form/prepare/my-action
hook allows you to stop the Action early if necessary.
It is also the best place to add some logic or create new query vars that could be used within the Action UI. Returning false
will stop the Action and go to the next one. Usage example:
/*
* Custom Prepare
*
* @bool $prepare True/false
* @array $form Form settings
* @string $post_id Post ID (where the form is displayed)
* @string $action Action name
*/
filter('acfe/form/prepare/my-action', $prepare, $form, $post_id, $action);
filter('acfe/form/prepare/my-action/form=my-form', $prepare, $form, $post_id, $action);
add_filter('acfe/form/prepare/my-action', 'my_form_prepare', 10, 4);
function my_form_prepare($prepare, $form, $post_id, $action){
// Get field input value
$my_field = get_field('my_field');
// Create custom message
$message = 'This is a custom message. Field value: ' . $my_field;
// Retrive the message using {query_var:email_message} in the UI
set_query_var('email_message', $message);
// Do not process the action if user isn't logged in
if(!is_user_logged_in()){
$prepare = false;
}
// return
return $prepare;
}
Trigger a custom action after the e-mail was sent using the acfe/form/submit/my-action
hook. Usage example:
/*
* Custom Submit
*
* @array $form Form settings
* @string $action Action name
*/
action('acfe/form/submit/my-action', $form, $action);
action('acfe/form/submit/my-action/form=my-form', $form, $action);
add_action('acfe/form/submit/my-action', 'my_form_submit', 10, 2);
function my_form_submit($form, $action){
// Get field input value
$my_field = get_field('my_field');
if($my_field === 'Company'){
// Do something...
}
}