最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

`save_post` hook called on inserting new post from front end

programmeradmin0浏览0评论

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)!

发布评论

评论列表(0)

  1. 暂无评论