Paulund
2013-05-06 #wordpress

Validate Featured Image On Post

Since Wordpress version 2.9 you have been able to add a post thumbnail to all your posts on your website. A post thumbnail is called a featured image for the posts, this image can be displayed in anyway you want, it is up to the theme developer. The benefit of this image is that it allows you to add an image to a post without it appearing in the content of the actual post. This will allow you to use an image to display the post instead of just the content. The main use of this image is to be used on the index page or the search results page of your Wordpress site.

Enable Theme Support For Featured Post

By default themes do not support this feature you need to add some code to the functions.php file.


add_theme_support( 'post-thumbnails' ); 

Set Featured Image

There is a meta box on the post screen where you can set an image to be the featured image on the post.

Display The Featured Image

To display the featured image on your Wordpress theme you need to use the function the_post_thumbnail(), but first you need to check if the post currently has a thumbnail image by using the has_post_thumbnail().


if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
    the_post_thumbnail();
}

Validate Featured Image On Post

If you theme requires that a post has a featured image then you need to make sure that your post has a featured image before you allow your users to publish the post. If you need to validate a post you can do this on the saving the post action save_post, this action will be ran when you save the current post. For us to validate this correctly we need to to check if the post has a post thumbnail by using the has_post_thumbnail() function. If the post doesn't have a post thumbnail then we need to set the post back to be a draft status and display an error message on the screen.

Validate Post Thumbnail

The following code can be used to validate the post thumbnail. This code will run on the save action of the post, first we check that the post type is for a post. If the post type is not a post then we return from the function so we don't continue. Then we check to see if the post has a thumbnail if it doesn't have a thumbnail then we set a new transient so that we can display an error message. Then we reset the post status to draft so the post doesn't displayed on the website. If the post has a thumbnail then we can just delete the transient so the error message isn't displayed anymore.

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

Next we use the admin_notices action to display an error message if the transient is set. Once the message is displayed then we can delete the transient.


add_action('admin_notices', 'pu_validate_thumbnail_error');
function pu_validate_thumbnail_error()
{
    // check if the transient is set, and display the error message
    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" );
    }
}

When you save the post and it doesn't have a featured image then you will see the post is set back to a draft and an error message is displayed on the screen.

UpChecker

Reliable uptime monitoring and instant alerts for any website downtime.

  • Uptime Monitoring
  • Performance Monitoring
  • SSL Certificate Alerts
  • Domain Monitoring
  • DNS Checker
  • XML Sitemap Monitoring