X

How To Require a Featured Image Before You Can Publish a Post

Snippets by IsItWP

Are you looking for a way to require a featured image before you can publish a post? This snippet will require that you have added a featured image before your post can be published. If you try to publish a post without a featured image after adding this snippet, you will receive this message: “You must select Featured Image. Your Post is saved but it can not be published.”

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

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 Featured Image. Your Post is saved but it can not be published.</strong></p></div>";
        delete_transient( "has_post_thumbnail" );
    }

}

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 7 Best WordPress Gallery Plugins Compared.

Comments  Leave a Reply

  1. Thanks

  2. Hey Kevin. I am creating a custom functions plugin where I added this snippet and it seems to work fine until I go to delete a draft/post. I am not able to delete as it just gives me the “You must select a Featured Image. Your Post is saved but it can not be published.” I have to remove the code to be able to delete a post.

    1. I put the function (wpds_check_thumbnail)’s code inside of:

      if ( ‘trash’ != get_post_status( $post_id ) ) { //Code here }

  3. Great! This is quite useful for me at the moment. Is this adjustable to only work on a certain post-type or category?

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!