How To Require A Featured Image In Post Before Publishing In WordPress

Having at least one image in your post in really good for search engine optimization, but it’s hard to get this thing in the mind of your authors and contributors. Here’s a snippet which would require a featured image in post before publishing it to your WordPress. An easy way to ensure all posts have a featured image.

When you try and publish a post without a featured image, you will get following error message:

[alert style=”red”]You must select Featured Image. Your Post is saved but it can not be published.[/alert]

Just drop the following snippet to your current theme’s functions.php file:

10 thoughts on “How To Require A Featured Image In Post Before Publishing In WordPress”

  1. This works but the problem with this is that it doesn’t allow us to move the draft post to trash just in case we changed our mind and decided to delete the featured-image-less post.

  2. Hi Hardeep, thanks for this code, it works wonderfully! My only issue is that I would like this message to appear only when the post is in specific categories (i.e: category number 21 & 45). Is there a way to show this message only when one of these categories is selected? Thanks a lot! :)

  3. Hi Hardeep, me again. I don’t know if you read my comment from last month, but I’ve tried to find a way to make the changes I need in your code. Since I’m not a coder, I have no idea how to do it, but it seems like this WordPress function might help:

    if (in_category(’21’, ’45’))

    So your code only works when the post is within one of those 2 categories. Let me know what you think about that, and thanks for your help! :)

  4. Hi Hardeep, thanks for your reply! In fact I just looked in the Codex and searched for a way to identify the categories a post belongs to. That’s what I got but I have no idea where to put it in your code…

    Since you can check if a featured image is set or not, why wouldn’t it be possible to check in the meantime, the post’s category? I don’t know how all this works, you’re the expert here, I rely on your knowledge, mine is close to none I’m afraid :(

    Thanks for your help!

  5. Hi Hardeep! How are you? I was just checking back and see if you had some time to find a way to make the thumbnail image mandatory only for specific categories. Let me know, thanks! :)

  6. Thanks for this function!

    I found it useful to wrap it in another conditional:

    if ( ( isset( $_POST[‘publish’] ) || isset( $_POST[‘save’] ) ) && $_POST[‘post_status’] == ‘publish’ ) {

    //set transient, etc

    }

    This way it only fires on publish, otherwise it will update to a draft every time you add a new post or try to trash a post.

Leave a Comment