The Post Action is a template to create or update a post on form submission.
| Setting name | Description |
| Action type | Create or update a post |
| Action name | Alias name allowing to use targeted hook |
| Target | The post to update |
| Source | The post to retrieve fields from |
| Load values | Fill inputs with values |
| Post type | The post type |
| Post status | The post status |
| Post slug | The post slug |
| Post content | The post content |
| Post author | The post author |
| Post date | The post date |
| Post thumbnail | The post thumbnail |
| Post parent | The parent post |
| Post terms | The post terms |
| Append terms | Append terms on submission |
| Save ACF fields | Choose which ACF fields should be saved to this post |
| 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_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');
}
}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;
}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;
}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)
// retrieve the 'name' specifically
$term_name = acfe_get_form_action('term.name');
// check the term name
if($term_name === 'My Term'){
// do something
}
// do not create/update post
// return false;
// return normally
return $args;
}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)
// retrieve the 'name' specifically
$term_name = acfe_get_form_action('term.name');
// check the term name
if($term_name === 'My Term'){
// do something
}
// manually update a field on the created/updated post
update_field('my_field', 'my_value', $post_id);
}The Action Output allows retrieve the action data in a future action within the Form UI using a Template Tag, or a PHP hook using a function.
For example, the Template Tag {action:post:post_title} will output the latest Post “Post Title”. The equivalent in PHP is acfe_get_form_action('post.post_title').
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;
}