Snippet: Require Post Thumbnail to be Uploaded Before Publishing
This short snippet requires authors and admins to set a post thumbnail before publishing any article. It will update the post and all of its contents but wont publish it until the thumbnail is set. For custom post types, you just need to change the “get_post_type” condition to your custom post type.
Snippet
add_action('save_post', 'wpds_check_thumbnail');
add_action('admin_notices', 'wpds_thumbnail_error');
function wpds_check_thumbnail($post_id) {
// change to any custom post type
if(get_post_type($post_id) != 'post')
return;
if ( !has_post_thumbnail( $post_id ) ) {
// set a transient to show the users an admin message
set_transient( "has_post_thumbnail", "no" );
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'wpds_check_thumbnail');
// update the post set it to draft
wp_update_post(array('ID' => $post_id, 'post_status' => 'draft'));
add_action('save_post', 'wpds_check_thumbnail');
} else {
delete_transient( "has_post_thumbnail" );
}
}
function wpds_thumbnail_error()
{
// check if the transient is set, and display the error message
if ( get_transient( "has_post_thumbnail" ) == "no" ) {
echo "<div id='message' class='error'><p><strong>You must select Post Thumbnail. Your Post is saved but it can not be published.</strong></p></div>";
delete_transient( "has_post_thumbnail" );
}
}







[...] This short snippet requires authors and admins to set a post thumbnail before publishing any article. It will update the post and all of its contents but wont publish it until the thumbnail is set. [...]
Perfect! Just what I was looking for.
How would I change this code to only apply to certain post formats. For instance, I am using “Standard, Gallery and Video” post formats. I would like this to only apply to the “Standard” post format.
http://codex.wordpress.org/Post_Formats
It would be pretty simple. Check for the “standard” post format in “wpds_check_thumbnail”. Paste the following code
if( get_post_format($post_id) === false) // no format is standard formatreturn;
after
if(get_post_type($post_id) != 'post')return;
where do i add this code?
Paste the code in your theme’s functions.php
How might I go about doing this for BOTH posts and pages?
You just need to alter the first condition “
if(get_post_type($post_id) != 'post')” and change it to followingif(get_post_type($post_id) != 'post' && get_post_type($post_id) != 'page')