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" );
    }

}

7 Responses to Snippet: Require Post Thumbnail to be Uploaded Before Publishing

  1. [...] 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.  [...]

  2. Marc says:

    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

    • Mohsin Rasool says:

      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 format
      return;

      after

      if(get_post_type($post_id) != 'post')
      return;

  3. brian says:

    where do i add this code?

  4. Kevin says:

    How might I go about doing this for BOTH posts and pages?

    • Mohsin Rasool says:

      You just need to alter the first condition “if(get_post_type($post_id) != 'post')” and change it to following

      if(get_post_type($post_id) != 'post' && get_post_type($post_id) != 'page')

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>