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 Video Library
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
View Events Video Library
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
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

Migrate, Modernize and Build Java Web Apps on Azure: This live workshop will cover methods to enhance Java application development workflow.

Modern Digital Website Security: Prepare to face any form of malicious web activity and enable your sites to optimally serve your customers.

E-Commerce Development Essentials: Considering starting or working on an e-commerce business? Learn how to create a backend that scales.

Related

  • Data-Driven Decision-Making in Product Management: The Key to Success
  • API Gateway Cache for POST Method
  • Streamlining Your Workflow With the Jenkins HTTP Request Plugin: A Guide to Replacing CURL in Scripts
  • Iptables Basic Commands for Novice

Trending

  • You’re Wasting Time With Your Daily Standup
  • Modern Application Performance
  • Quarkus 3: The Future of Java Microservices With Virtual Threads and Beyond
  • CI/CD Software Design Patterns and Anti-Patterns

Validate Featured Image On Post

Paul Underwood user avatar by
Paul Underwood
·
May. 07, 13 · Interview
Like (0)
Save
Tweet
Share
4.9K 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.

Related

  • Data-Driven Decision-Making in Product Management: The Key to Success
  • API Gateway Cache for POST Method
  • Streamlining Your Workflow With the Jenkins HTTP Request Plugin: A Guide to Replacing CURL in Scripts
  • Iptables Basic Commands for Novice

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
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: