The Option Action is a template to create or update options on form submission.
| Setting name | Description |
| Action name | Alias name allowing to use targeted hook |
| Target | The options page to update |
| Source | The options page to retrieve fields from |
| Load values | Fill inputs with values |
| Save ACF fields | Choose which ACF fields should be saved to this options page |
| Load ACF fields | Choose which ACF fields should have their values loaded |
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_option hook and throw errors with the acfe_add_validation_error() function. Usage example:
/**
* Option Action: Validation
*
* @array $form Form settings array
* @array $action Action settings array
*/
action('acfe/form/validate_option', $form, $action);
action('acfe/form/validate_option/form=my-form', $form, $action);
action('acfe/form/validate_option/action=my-option', $form, $action);add_action('acfe/form/validate_option/form=my-form', 'my_option_validation', 10, 2);
function my_option_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:
/**
* Option Action: Prepare
*
* @array $action Action settings array
* @array $form Form settings array
*
* @return array|false
*/
filter('acfe/form/prepare_option', $action, $form);
filter('acfe/form/prepare_option/form=my-form', $action, $form);
filter('acfe/form/prepare_option/action=my-option', $action, $form);add_filter('acfe/form/prepare_option/form=my-form', 'my_option_prepare', 10, 2);
function my_option_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;
}Change the option ID where meta values are loaded from. Usage example:
/**
* Option Action: Values Source
*
* @string $option_id Option ID source
* @array $form Form settings array
* @array $action Action settings array
*
* @return string
*/
filter('acfe/form/load_option_id', $option_id, $form, $action);
filter('acfe/form/load_option_id/form=my-form', $option_id, $form, $action);
filter('acfe/form/load_option_id/action=my-option', $option_id, $form, $action);
add_filter('acfe/form/load_option_id/form=my-form', 'my_option_load_id', 10, 3);
function my_option_load_id($option_id, $form, $action){
// get field input value
$my_field = get_field('my_field');
// check field value
// and force to load values from the options post id 'my_options'
if($my_field === 'Company'){
$option_id = 'my_options';
}
// return normally
return $option_id;
}Change the option ID before database insert/update. Usage example:
/**
* Option Action: Target
*
* @string $option_id Targeted Option ID
* @array $form Form settings array
* @array $action Action settings array
*
* @return string
*/
filter('acfe/form/submit_option_id', $option_id, $form, $action);
filter('acfe/form/submit_option_id/form=my-form', $option_id, $form, $action);
filter('acfe/form/submit_option_id/action=my-option', $option_id, $form, $action);add_filter('acfe/form/submit_option_id/form=my-form', 'my_option_submit_id', 10, 3);
function my_option_submit_id($option_id, $form, $action){
// get the form input value named 'my_field'
$my_field = get_field('my_field');
// check field value
// and change the option post id
if($my_field === 'Company'){
$option_id = 'my_options';
}
// 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
// and change the option post id
if($post_title === 'My Post'){
$option_id = 'my_options';
}
// return normally
return $option_id;
}Trigger a custom action after the option was created/updated using the acfe/form/submit_option hook. Usage example:
/**
* Option Action: Submit
*
* @string $option_id Targeted Option Post ID
* @array $form Form settings array
* @array $action Action settings array
*/
action('acfe/form/submit_option', $option_id, $form, $action);
action('acfe/form/submit_option/form=my-form', $option_id, $form, $action);
action('acfe/form/submit_option/action=my-option', $option_id, $form, $action);add_action('acfe/form/submit_option/form=my-form', 'my_option_submit', 10, 3);
function my_option_submit($option_id, $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
}
}