I have a small plugin that implements a custom post type and some meta data. Everything works but if I save the custom post type, then trash it, then click undo, I get the error I have set up to check if the nonce is set. The error is thrown, but the post is still removed from the trash. But I want to know the best way to prevent the error happening. This is how I've implemented it:
I have used the post_submitbox_start
hook to add a nonce to the admin page for entering the metadata.
public function register(){
add_action( 'post_submitbox_start', array($this,'add_cpt_nonce') );
}
function add_cpt_nonce($post)
{
wp_nonce_field( '_save_cpt'.$post->ID, 'my_cpt_nonce' );
}
When the post is saved, I have a function to validate the metadata. Included in this function is a check that the nonce is set, if it is not set, it throws an error. This is the error being thrown when I undo a trashed post.
function save_cpt_meta_data($post_id, $post)
{
if (in_array($post->post_status, array('auto-draft', 'trash' , 'draft'))) {
// exit, nonce will not be set
return;
}
// check nonce is set -- THIS IS THE ERROR BEING THROWN
if( !isset( $_POST['my_cpt_nonce'] ))
{
$status = $post->post_status;
return wp_die("Nonce error.");
}
//...
}
What is the best to handle the undoing of trashed custom post types and circumvent this check / error for the nonce.