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.

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 Post action

(Optional) Target this action using hooks.

Click to initialize TinyMCE

Fill inputs with values

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 dateThe post date
Post thumbnailThe post thumbnail
Post parentThe parent post
Post termsThe post terms
Append termsAppend terms on submission
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/validate_post hook and throw errors with the acfe_add_validation_error() function. Usage example:

/**
 * Post Action: Validation
 * 
 * @array  $form    Form settings array
 * @array  $action  Action settings array
 */
 
action('acfe/form/validate_post',                $form, $action);
action('acfe/form/validate_post/form=my-form',   $form, $action);
action('acfe/form/validate_post/action=my-post', $form, $action);
add_action('acfe/form/validate_post/form=my-form', 'my_post_validation', 10, 2);
function my_post_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:

/**
 * Post Action: Prepare
 * 
 * @array  $action  Action settings array
 * @array  $form    Form settings array
 *
 * @return array|false
 */
 
filter('acfe/form/prepare_post',                $action, $form);
filter('acfe/form/prepare_post/form=my-form',   $action, $form);
filter('acfe/form/prepare_post/action=my-post', $action, $form);
add_filter('acfe/form/prepare_post/form=my-form', 'my_post_prepare', 10, 2);
function my_post_prepare($action, $form){
    
    // if user isn't logged in
    // do not create/update the post
    if(!is_user_logged_in()){
        return false;
    }
    
    // return normally
    return $action;
    
}

Values Source

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

/**
 * Post Action: Values Source
 * 
 * @int    $post_id  Post ID source
 * @array  $form     Form settings array
 * @array  $action   Action settings array
 *
 * @return int
 */

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_post_load_id', 10, 3);
function my_post_load_id($post_id, $form, $action){
    
    // get field input value
    $my_field = get_field('my_field');
    
    // check field value
    // and force to load values from the post id 145
    if($my_field === 'Company'){
        $post_id = 145;
    }
    
    // return normally
    return $post_id;
    
}

Post Arguments

Change the post arguments before database insert/update. Those arguments are later passed to wp_insert_post() or wp_update_post() (See documentation). Usage example:

/**
 * Post Action: Arguments
 * 
 * @array  $args    Post arguments, later passed to wp_insert_post()
 * @array  $form    Form settings array
 * @array  $action  Action settings array
 * 
 * @return array|false
 */

filter('acfe/form/submit_post_args',                $args, $form, $action);
filter('acfe/form/submit_post_args/form=my-form',   $args, $form, $action);
filter('acfe/form/submit_post_args/action=my-post', $args, $form, $action);
add_filter('acfe/form/submit_post_args/form=my-form', 'my_post_submit_args', 10, 3);
function my_post_submit_args($args, $form, $action){

    // get current post id
    // where the form is displayed
    $post_id = $form['post_id'];

    // get the action type
    // either 'insert_post' or 'update_post'
    $action_type = $action['type'];
    
    // get the form input value
    $my_field = get_field('my_field');
    
    // check field value
    // and change the post title
    if($my_field === 'Company'){
        $args['post_title'] = 'Company';
    }

    // get previous term action output (if any)
    $term_action = acfe_form_get_action('term');

    // check previous term action exists
    if(!empty($term_action)){
    
        // check the term name of the created term
        if($term_action['name'] === 'Term Name'){
            // do something
        }

    }

    // do not create/update post
    // return false;
    
    // return normally
    return $args;
    
}

Submit

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

/**
 * Post Action: Submit
 * 
 * @int    $post_id  Created/Updated post ID
 * @array  $args     Post arguments
 * @array  $form     Form settings array
 * @array  $action   Action settings array
 */
 
action('acfe/form/submit_post',                $post_id, $args, $form, $action);
action('acfe/form/submit_post/form=my-form',   $post_id, $args, $form, $action);
action('acfe/form/submit_post/action=my-post', $post_id, $args, $form, $action);
add_action('acfe/form/submit_post/form=my-form', 'my_post_submit', 10, 4);
function my_post_submit($post_id, $args, $form, $action){

    // get current post id
    // where the form is displayed
    $current_post_id = $form['post_id'];

    // get the action type
    // either 'insert_post' or 'update_post'
    $action_type = $action['type'];
    
    // get the form input value
    $my_field = get_field('my_field');
    
    // check field value
    if($my_field === 'Company'){
        // do something
    }

    // get previous term action output (if any)
    $term_action = acfe_form_get_action('term');

    // check previous term action exists
    if(!empty($term_action)){
    
        // check the term name of the created term
        if($term_action['name'] === 'Term Name'){
            // do something
        }

    }

    // manually update a field on the created/updated post
    update_field('my_field', 'my_value', $post_id);
    
}

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/submit_post_output filter. Usage example:

/**
 * Post Action: Output
 * 
 * @array  $output   Output information
 * @array  $args     Post arguments
 * @array  $form     Form settings array
 * @array  $action   Action settings array
 * 
 * @return array
 */
 
filter('acfe/form/submit_post_output',                $output, $args, $form, $action);
filter('acfe/form/submit_post_output/form=my-form',   $output, $args, $form, $action);
filter('acfe/form/submit_post_output/action=my-post', $output, $args, $form, $action);
add_filter('acfe/form/submit_post_output/form=my-form', 'my_post_output', 10, 4);
function my_post_output($output, $args, $form, $action){
    
    /**
     * $output = 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');
    
    // check field value
    if($my_field === 'Company'){
    
        // set a custom key/value pair in the output
        $output['custom_key'] = 'custom_value';
    
    }
    
    // return normally
    return $output;
    
}