Require a featured image to WordPress posts

If you own a multi-author Wordpress blog or website and you want to force a required featured image for all your posts then the script below is all that you need. Adding this snippet to your functions.php will make sure that all your posts have a featured image before they are published.

Basically, the snippet is a PHP function wrapped in a WordPress action. When you hit the publish button it checks if the author has associated a featured image to his post. If no featured image has been assigned to the post, the function displays an error message to the user and advises him to add one in order for the post to be published.

PHP
add_action('admin_notices', 'wph_featured_image_msg');
function wph_require_featured_image($post_id) {
    if(get_post_type($post_id) != 'post')
        return;
    if ( !has_post_thumbnail( $post_id ) ) {
        set_transient( "has_post_thumbnail", "no" );
        remove_action('save_post', 'wph_require_featured_image');
        wp_update_post(array('ID' => $post_id, 'post_status' => 'draft'));
        add_action('save_post', 'wph_require_featured_image');
    } else {
        delete_transient( "has_post_thumbnail" );
    }
}
function wph_featured_image_msg()
{
    if ( get_transient( "has_post_thumbnail" ) == "no" ) {
        echo "<div id='message' class='error'><p><strong>Please assign a featured image to your post to be published.</strong></p></div>";
        delete_transient( "has_post_thumbnail" );
    }
}

Source

Please note that this snippet can be used to require a featured image for pages and for custom post types as well.

For example, if you have a custom post type named ‘book’ and you wish to force the use of a featured image for it then just update the third line in the snippet as follows:

PHP
if(get_post_type($post_id) != 'book')