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.
1 Answer
Reset to default 1Yes 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)!