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.


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

  • Is save_post_{post_type} intended to update a post type from backend only ? Can the same hook be used to save data from front end ? The reason i am asking is that , of all the tutorials i have seen, they just access $_POST and use the posted data to insert a post using wp_insert_post – Shadow Sep 4 '19 at 9:31
  • 1
    Good question. No, it is not intended to be used on the backend only. It is, perhaps, a more granular approach than wp_insert_post(). I think the main difference, as I said in my post, is that save_post_{post_type} runs every time the post is saved, whether it is being inserted or being updated. This can trip you up quite easily and is perhaps why most tutorials use wp_insert_post(). – Ted Stresen-Reuter Sep 4 '19 at 9:38
  • Thanks a lot for the information. This is something that most tutorials lack. – Shadow Sep 4 '19 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.