admin管理员组

文章数量:1414908

I have a front end form which creates a new post in a custom post type and adds some meta data to the post. I am submitting the form by checking for $_POST values in the same page in which form is created.

if( isset($_POST['save_form']) ){
    $post_info = ''; // Set values to variables
    $post_id = wp_insert_post( $post_info );
    add_post_meta($post_id, 'my_key', $form_field, true);
}

Recently i have found that save_post_{post_type} hook is fired whenever i submit this form. So all the fields in the front end form are available inside the callback function. Is this default or have i done something wrong ? Should i change the saving method since posted values are available in save_post_{post_type} hook.

I have a front end form which creates a new post in a custom post type and adds some meta data to the post. I am submitting the form by checking for $_POST values in the same page in which form is created.

if( isset($_POST['save_form']) ){
    $post_info = ''; // Set values to variables
    $post_id = wp_insert_post( $post_info );
    add_post_meta($post_id, 'my_key', $form_field, true);
}

Recently i have found that save_post_{post_type} hook is fired whenever i submit this form. So all the fields in the front end form are available inside the callback function. Is this default or have i done something wrong ? Should i change the saving method since posted values are available in save_post_{post_type} hook.

Share Improve this question asked Sep 4, 2019 at 6:41 ShadowShadow 571 silver badge7 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Yes it is the default and yes you should be doing your work inside the save_post_{post_type} action. That's exactly what it is there for and is its intended use. Be careful, though, as this action is called every time the post is "saved" (created or updated)!

本文标签: savepost hook called on inserting new post from front end