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/validate_my-action hook and throw errors with the acfe_add_validation_error() function. Usage example:
/**
* Custom Action: Validation
*
* @array $form Form settings array
* @array $action Action settings array
*/
action('acfe/form/validate_my-action', $form, $action);
action('acfe/form/validate_my-action/form=my-form', $form, $action);add_action('acfe/form/validate_my-action', 'my_form_validation', 10, 2);
function my_form_validation($form, $action){
// get current post id
// where the form is displayed
$post_id = $form['post_id'];
// get field input value
$my_field = get_field('my_field');
// check field value
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. Returning false will stop the Action and go to the next one. Usage example:
/**
* Custom Action: Prepare
*
* @array $action Action settings array
* @array $form Form settings array
*
* @return array|false
*/
filter('acfe/form/prepare_my-action', $action, $form);
filter('acfe/form/prepare_my-action/form=my-form', $action, $form);add_filter('acfe/form/prepare_my-action', 'my_form_prepare', 10, 2);
function my_form_prepare($action, $form){
// if user isn't logged in
// do not submit the action
if(!is_user_logged_in()){
return false;
}
// return normally
return $action;
}Trigger a custom submission action using the acfe/form/submit_my-action hook. Usage example:
/**
* Custom Action: Submit
*
* @array $form Form settings array
* @array $action Action settings array
*/
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 current post id
// where the form is displayed
$post_id = $form['post_id'];
// get field input value
$my_field = get_field('my_field');
// check field value
if($my_field === 'Company'){
// do something
}
// get previous post action output (if any)
// retrieve the 'post_title' specifically
$post_title = acfe_get_form_action('post.post_title');
// check the post title
if($post_title === 'My Post'){
// do something
}
}