Created
September 26, 2014 10:23
-
-
Save alessandrotesoro/4e119a97367bcb88a639 to your computer and use it in GitHub Desktop.
Example of throwing errors during post creation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action('save_post', 'wdb_check_thumbnail'); | |
add_action('admin_notices', 'wdb_thumbnail_error'); | |
function wdb_check_thumbnail($post_id) { | |
// convert to a custom post type | |
if(get_post_type($post_id) != 'post') | |
return; | |
if ( !has_post_thumbnail( $post_id ) ) { | |
// Show the users an admin message | |
set_transient( "has_post_thumbnail", "no" ); | |
remove_action('save_post', 'wdb_check_thumbnail'); | |
// make the post a draft | |
wp_update_post(array('ID' => $post_id, 'post_status' => 'draft')); | |
add_action('save_post', 'wdb_check_thumbnail'); | |
} else { | |
delete_transient( "has_post_thumbnail" ); | |
} | |
} | |
function wdb_thumbnail_error() | |
{ | |
if ( get_transient( "has_post_thumbnail" ) == "no" ) { | |
echo "<div id='message' class='error'><p>You must select featured image in order for your post to be published.</p></div>"; | |
delete_transient( "has_post_thumbnail" ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment