WordPress default featured image

October 3, 2012 | PHP, Time Savers, Troubleshooting, Tutorials, Wordpress | 0 comments

I have often wondered what to do in WordPress if no featured image is used for a page or post. Normally I just use a getter and if it’s empty then I will use an image defined in the template. This is ok for new sites although not as efficient as this method which uses a WordPress filter to check for the existence of the image and if it doesn’t exist will return the placeholder instead.

This way the code is central and can be changed easily. What it doesn’t yet do is differentiate the size of the image by the size name and return the appropriate sized placeholder. Should be easy enough to extend though:

add_filter('post_thumbnail_html', 'sb_post_thumbnail_html', 100, 5);
function sb_post_thumbnail_html($html, $post_id, $post_thumbnail_id, $size, $attr) {
 if (!$html) {
 $html = '<img width="150" height="150" src="http://placehold.it/150x150" class="attachment-thumbnail wp-post-image" />';
 }
 
 return $html;
}

A Donate Button!

0 Comments

Submit a Comment

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

CommentLuv badge

Stay in touch!

Page Builder Cloud

Page Builder Cloud

A truly universal template library for WordPress Page Builders.

Page Builder Recommendation

Elementor Banner

We are BIG fans of the Elementor page builder. Give it a look!

About this site and Sean Barton

Picture of Sean
Sean Barton is a Freelance PHP Website Developer in Crewe, Cheshire. He is a Wordpress and CMS/Framework specialist and Co-Founder of Page Builder Cloud.
This site was set up in 2008 as a tutorial and scripting resource for the PHP language and Wordpress.
Find out more about Sean on the About Me page or use the Hire Me page to get in touch. For more information about Sean's work take a look at the Portfolio

Our Services

  • Wordpress plugin/theme development
  • Divi specialist
  • Ecommerce (Woocommerce, WPSC, Shopify, Magento)
  • PSD to Wordpress theme conversion (Responsive)
  • Website design work (Banners, Logos, Full Site, etc)
  • Website analysis (security, usability, SEO)
  • API Integrations (InfusionSoft, SalesForce, Ontraport, Customer Thermometer, etc..)
  • Wordpress consultancy & expert advice
  • Crisis support
  • Website hosting

The main services offered are Wordpress based although we do a great deal of technical programming for bespoke systems. From troubleshooting, extending frameworks, finding bugs to writing them from scratch.

Find out more by looking through our past projects or get a quote.

Be the first to hear about new products/updates!

This is a mailing list for those people interested in being told when we release a new product (Divi plugin or Theme).

We shall also use this list to let you know about product updates and releases.

You have Successfully Subscribed!