Option Action

The Option Action is a template to create or update options on form submission.

Dynamic Form

Add actions on form submission

Click the "Add action" button below to start creating your layout
0 Custom action

Set a unique action slug.

0 Email action
0 Option action

(Optional) Target this action using hooks.

Fill inputs with values

0 Post action

(Optional) Target this action using hooks.

Click to initialize TinyMCE

Fill inputs with values

0 Redirect action

(Optional) Target this action using hooks.

The URL to redirect to. See "Cheatsheet" tab for all available template tags.

0 Term action

(Optional) Target this action using hooks.

Click to initialize TinyMCE

Fill inputs with values

0 User action

(Optional) Target this action using hooks.

Click to initialize TinyMCE

Fill inputs with values

1 Option action

(Optional) Target this action using hooks.

Fill inputs with values

Option Settings

Setting nameDescription
Action nameAlias name allowing to use targeted hook
TargetThe options page to update
SourceThe options page to retrieve fields from
Load valuesFill inputs with values
Save ACF fieldsChoose which ACF fields should be saved to this options page
Load ACF fieldsChoose which ACF fields should have their values loaded

Validation

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');
        
    }
    
}

Prepare

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;
    
}

Values Source

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;
    
}

Option Target

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;
    
}

Submit

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
    }
    
}