DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Explainable AI: Making the Black Box Transparent
  • Demystifying SPF Record Limitations
  • Integration Architecture Guiding Principles, A Reference
  • Database Integration Tests With Spring Boot and Testcontainers

Trending

  • Explainable AI: Making the Black Box Transparent
  • Demystifying SPF Record Limitations
  • Integration Architecture Guiding Principles, A Reference
  • Database Integration Tests With Spring Boot and Testcontainers

Validate Featured Image On Post

Paul Underwood user avatar by
Paul Underwood
·
May. 07, 13 · Interview
Like (0)
Save
Tweet
Share
4.84K Views

Join the DZone community and get the full member experience.

Join For Free

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 functiona.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.

featured_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'>
<strong>A post thumbnail must be set before saving the post.</strong>
</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.





POST (HTTP)

Published at DZone with permission of Paul Underwood, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Explainable AI: Making the Black Box Transparent
  • Demystifying SPF Record Limitations
  • Integration Architecture Guiding Principles, A Reference
  • Database Integration Tests With Spring Boot and Testcontainers

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: