Due to auto-save’s the post will always exist in WordPress, it’s inserted into the database as soon as content is entered. So you can’t prevent an entry, – however you can filter after a user hits “Publish” or “Save” on the post using wp_insert_post
, or save_post
. With this action you can do your conditional checking and change the post_status from publish to draft, or whatever is needed for your plugin.
For example:
function _myplugin_save_post_check($post_id) {
// If this is a revision, get real post ID
if ( $parent_id = wp_is_post_revision( $post_id ) )
$post_id = $parent_id;
// Do whatever conditional checks you want here, like checking the custom feilds values
$custom_feilds = get_post_meta( $post_id, 'whatever', true );
if ( empty( $custom_feilds ) ) {
// unhook this function so it doesn't loop infinitely
remove_action( 'save_post', '_myplugin_save_post_check' );
// update the post as unpublished, or whatever
wp_update_post( array( 'ID' => $post_id, 'post_status' => 'draft' ) );
// re-hook this function
add_action( 'save_post', '_myplugin_save_post_check' );
}
}
add_action( 'save_post', '_myplugin_save_post_check' );
The above will make the post a draft if the custom meta field 'whatever'
is empty.