Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@alessandrotesoro
Created September 26, 2014 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alessandrotesoro/4e119a97367bcb88a639 to your computer and use it in GitHub Desktop.
Save alessandrotesoro/4e119a97367bcb88a639 to your computer and use it in GitHub Desktop.
Example of throwing errors during post creation
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