Post Action

The Post Action is a template to create or update a post 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.

Choose which ACF fields should be saved to this option

Fill inputs with values

Choose which ACF fields should have their values loaded

0 Post action

(Optional) Target this action using hooks.

Click to initialize TinyMCE

Choose which ACF fields should be saved to this post

Fill inputs with values

Choose which ACF fields should have their values loaded

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

Choose which ACF fields should be saved to this term

Fill inputs with values

Choose which ACF fields should have their values loaded

0 User action

(Optional) Target this action using hooks.

Click to initialize TinyMCE

Choose which ACF fields should be saved to this user

Fill inputs with values

Choose which ACF fields should have their values loaded

1 Post action

(Optional) Target this action using hooks.

Click to initialize TinyMCE

Choose which ACF fields should be saved to this post

Fill inputs with values

Choose which ACF fields should have their values loaded

Post Settings

Setting nameDescription
Action typeCreate or update a post
Action nameAlias name allowing to use targeted hook
TargetThe post to update
SourceThe post to retrieve fields from
Load valuesFill inputs with values
Post typeThe post type
Post statusThe post status
Post slugThe post slug
Post contentThe post content
Post authorThe post author
Post parentThe parent post
Post termsThe post terms
Save ACF fieldsChoose which ACF fields should be saved to this post
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/validation/post hook and throw errors with the acfe_add_validation_error() function. Usage example:

/*
 * Post Validation
 * 
 * @array   $form     Form settings
 * @string  $post_id  Post ID (where the form is displayed)
 * @string  $action   Action name
 */
 
action('acfe/form/validation/post',                $form, $post_id, $action);
action('acfe/form/validation/post/form=my-form',   $form, $post_id, $action);
action('acfe/form/validation/post/action=my-post', $form, $post_id, $action);
add_action('acfe/form/validation/post/form=my-form', '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');
        
    }
    
}

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:

/*
 * Post 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/post',                $prepare, $form, $post_id, $action);
filter('acfe/form/prepare/post/form=my-form',   $prepare, $form, $post_id, $action);
filter('acfe/form/prepare/post/action=my-post', $prepare, $form, $post_id, $action);
add_filter('acfe/form/prepare/post/form=my-form', 'my_form_prepare', 10, 4);
function my_form_prepare($prepare, $form, $post_id, $action){
    
    // Do not create/update post if user isn't logged in
    if(!is_user_logged_in()){
        
        $prepare = false;
        
    }
    
    // return
    return $prepare;
    
}

Values Source

Alter the post ID where meta values are loaded from. Usage example:

/*
 * Post Values Source
 * 
 * @int     $post_id  Post ID Source
 * @array   $form     Form settings
 * @string  $action   Action name
 */

filter('acfe/form/load/post_id',                $post_id, $form, $action);
filter('acfe/form/load/post_id/form=my-form',   $post_id, $form, $action);
filter('acfe/form/load/post_id/action=my-post', $post_id, $form, $action);
add_filter('acfe/form/load/post_id/form=my-form', 'my_form_post_source', 10, 3);
function my_form_post_source($post_id, $form, $action){
    
    // Get field input value
    $my_field = get_field('my_field');
    
    if($my_field === 'Company'){
    
        // Force to load values from the post ID 145
        $post_id = 145;
    
    }
    
    // return
    return $post_id;
    
}

Post Arguments

Alter the post arguments before database insert/update. Usage example:

/*
 * Post Arguments
 * 
 * @array   $args    Generated post arguments
 * @string  $type    Action type: 'insert_post' or 'update_post'
 * @array   $form    Form settings
 * @string  $action  Action name
 */

filter('acfe/form/submit/post_args',                $args, $type, $form, $action);
filter('acfe/form/submit/post_args/form=my-form',   $args, $type, $form, $action);
filter('acfe/form/submit/post_args/action=my-post', $args, $type, $form, $action);
add_filter('acfe/form/submit/post_args/form=my-form', 'my_form_post_args', 10, 4);
function my_form_post_args($args, $type, $form, $action){
    
    /*
     * Get the form input value named 'my_field'
     */
    $my_field = get_field('my_field');

    if($my_field === 'Company'){
    
        // Change Post Title
        $args['post_title'] = 'Company';
    
    }

    /*
     * Get previous Post Action output
     */
    $prev_post_action = acfe_form_get_action('post');
    
    if(!empty($prev_post_action)){
    
        if($prev_post_action['post_title'] === 'Company'){
            
            // Change Post Title
            $args['post_title'] = 'Company';
            
        }

    }
    
    // Stop post creation/update
    // return false;
    
    // return
    return $args;
    
}

Submit

Trigger a custom action after the post was created/updated using the acfe/form/submit/post hook. Usage example:

/*
 * Post Submit
 * 
 * @int     $post_id  Created/Updated post ID
 * @string  $type     Action type: 'insert_post' or 'update_post'
 * @array   $args     Generated post arguments
 * @array   $form     Form settings
 * @string  $action   Action name
 */
 
action('acfe/form/submit/post',                $post_id, $type, $args, $form, $action);
action('acfe/form/submit/post/form=my-form',   $post_id, $type, $args, $form, $action);
action('acfe/form/submit/post/action=my-post', $post_id, $type, $args, $form, $action);
add_action('acfe/form/submit/post/form=my-form', 'my_form_submit', 10, 5);
function my_form_submit($post_id, $type, $args, $form, $action){
    
    /*
     * Get the form input value named 'my_field'
     */
    $my_field = get_field('my_field');

    if($my_field === 'Company'){
    
        // do_something();
        
    }

    /*
     * Get previous Post Action output
     */
    $prev_post_action = acfe_form_get_action('post');
    
    if(!empty($prev_post_action)){
        
        if($prev_post_action['post_title'] === 'Company'){
            
            // do_something();
            
        }
        
    }
    
}

Output

The Action Output let developers retrieve the action data in a future action within the Form UI or a PHP hook. For example, the Template Tag {action:post:post_title} will output the latest Post Action “Title” setting.

If the action is named, the Template Tag will use it. Ie: {action:my-post:post_title}. The equivalent function in PHP is acfe_form_get_action('post').

It is possible to change the output of a Post Action using the acfe/form/output/post filter. Usage example:

/*
 * Post Output
 * 
 * @array   $data     Post information
 * @array   $post_id  Created/Updated post ID
 * @string  $type     Action type: 'insert_post' or 'update_post'
 * @array   $args     Post arguments
 * @array   $form     Form settings
 * @string  $action   Action name
 */
 
filter('acfe/form/output/post',                $data, $post_id, $type, $args, $form, $action);
filter('acfe/form/output/post/form=my-form',   $data, $post_id, $type, $args, $form, $action);
filter('acfe/form/output/post/action=my-post', $data, $post_id, $type, $args, $form, $action);
add_filter('acfe/form/output/post/form=my-form', 'my_form_output', 10, 6);
function my_form_output($data, $post_id, $type, $args, $form, $action){
    
    /*
     * $data
     *
     * Array
     * (
     * [ID] => 1554
     * [post_author] => 1
     * [post_date] => 2020-08-19 20:53:31
     * [post_date_gmt] => 0000-00-00 00:00:00
     * [post_content] =>
     * [post_title] => My Post
     * [post_excerpt] =>
     * [post_status] => publish
     * [comment_status] => open
     * [ping_status] => open
     * [post_password] =>
     * [post_name] =>
     * [to_ping] =>
     * [pinged] =>
     * [post_modified] => 2020-08-19 20:53:31
     * [post_modified_gmt] => 2020-08-19 18:53:31
     * [post_content_filtered] =>
     * [post_parent] => 0
     * [guid] => /?p=1554
     * [menu_order] => 0
     * [post_type] => page
     * [post_mime_type] =>
     * [comment_count] => 0
     * [filter] => raw
     * [ancestors] => Array()
     * [page_template] =>
     * [post_category] => Array()
     * [tags_input] => Array()
     * [permalink] => /my-post
     * [admin_url] => /wp-admin/post.php?post=1554&action=edit
     * [post_author_data] => Array
     *      (
     *          [ID] => 1
     *          [user_login] => user
     *          [user_pass] => ##password_hash##
     *          [user_nicename] => user
     *          [user_email] => [email protected]
     *          [user_url] =>
     *          [user_registered] => 2020-03-07 08:52:03
     *          [user_activation_key] =>
     *          [user_status] => 0
     *          [display_name] => User
     *          [nickname] => User
     *          [first_name] =>
     *          [last_name] =>
     *          [description] =>
     *          [rich_editing] => true
     *          [syntax_highlighting] => true
     *          [comment_shortcuts] => false
     *          ...
     *      )
     * )
     */
    
    // Get field input value
    $my_field = get_field('my_field');
    
    if($my_field === 'Company'){
    
        // Set a custom key
        $data['custom_key'] = 'custom_value';
    
    }
    
    // return
    return $data;
    
}