When you choose “Your CPT > Add New”, WP calls get_default_post_to_edit(), which actually creates an “empty” post (with 'post_status' => 'auto-draft'
) and then calls wp_insert_post(). This is what is causing your save_datasheet_meta()
function to be called before you think it should.
Hence, generally you should add some additional sanity checks to the beginning of any func you hook into save_post
, ala:
function
save_datasheet_meta ($post_id, $post)
{
if ('auto-draft' == $post->post_status) {
// bail, because your nonce will not be set at this point
return ;
}
// post{,-new}.php enqueue some JS which periodically saves the post_content,
// but NOT any post_meta
// so, a `save_post` hook func that only cares about updating post_meta should
// bail when DOING_AUTOSAVE is true
if (defined ('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return ;
}
$nonce = wp_verify_nonce ( plugin_basename(__FILE__), 'datasheet_meta_nonce');
if (!$nonce) {
die ("Security check failed");
}
// save post_meta's here
return ;
}