Make your authors select featured images before publishing posts

When WordPress reached version 2.9, people got a chance to add featured images to their posts. Since then, if themes allowed it, you were able to upload or select an image for your post which would nicely represent it and you still weren’t obligated to show that picture within the post.

WordPress users quickly got accustomed to the new feature and now it has become hard to find a theme without featured images.

But if you publish a lot and have more than one author, sometimes you can forget to add a featured image which can result in a messy website. Your post without image would look empty and unfinished on the homepage, there would definitely be a hole made in your recent posts widget, and if you use thumbnail to show the picture in the beginning of your article, your post would look very plain without a picture.

Those are just few simple problems you might have if you forget to include a featured image.  Before those problems even come up, let’s see how to make it disappear forever.

In this article, we’re about to show you a piece of code which will let you forget about the problem. Once you install the code, every author who tries to publish a post without having a featured image will get an error message. His or her post will then end up being saved as draft instead of being published. This will make that particular author use any featured image before publishing a post and will be a life saver when you’re in a rush.

If you’re ready to make the function work, here are the steps you should follow:

  1. Open your functions.php file
  2. Copy and paste the following code:
  3. add_action('save_post', 'pu_validate_thumbnail');
    
    function pu_validate_thumbnail($post_id)
    {
    // Only validate post type of post
    if(get_post_type($post_id) != 'post')
    return;
    
    // Check post has a thumbnail
    if ( !has_post_thumbnail( $post_id ) ) {
    
    // Confirm validate thumbnail has failed
    set_transient( "pu_validate_thumbnail_failed", "true" );
    
    // Remove this action so we can resave the post as a draft and then reattach the post
    remove_action('save_post', 'pu_validate_thumbnail');
    
    wp_update_post(array('ID' => $post_id, 'post_status' => 'draft'));
    add_action('save_post', 'pu_validate_thumbnail');
    } else {
    
    // If the post has a thumbnail delete the transient
    delete_transient( "pu_validate_thumbnail_failed" );
    
    }
    }
  4. Save changes

This code will check the post status once author clicks on the publish button. It works for posts only and if the function doesn’t find a thumbnail attached to it, a draft would be saved and WordPress would be prepared for showing an error message. If a thumbnail is found, a post is normally published without showing the message.

Since the code shown above doesn’t contain the error message itself, you still need a little code snippet which will prepare it:

  1. Right after the previous code, copy and paste this one:
  2. add_action('admin_notices', 'pu_validate_thumbnail_error');
    function pu_validate_thumbnail_error()
    {
    if ( get_transient( "pu_validate_thumbnail_failed" ) == "true" ) {
    echo "<div id='message' class='error'><p><strong>A post thumbnail must be set before saving the post.</strong></p></div>";
    delete_transient( "pu_validate_thumbnail_failed" );
    }
    }
  3. Save changes and try your new function

If you want to personalize the error message, navigate to row #5 and change the “A post thumbnail must be set before saving the post” to anything you like.

That’s it. Enjoy your posts without having to worry about not having a featured image.

START YOUR OWN BLOG

This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

Leave a Reply

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