in Coding, WordPress

Default post thumbnails in WordPress

There is regular discussion within the WordPress user community on certain common encounters developers have when creating themes and plugins. One such discussion is around post thumbnails 1 and, more specifically, how to specify a default thumbnail. After reading a few discussions around this, I thought I’d share my take on things.

There are a myriad of methods to achieve this. Some developers do conditional checks in their code to see if an entry has an image assigned to it. This is the most common method of applying a default thumbnail. If the entry doesn’t have a thumbnail, display a default image. That’s pretty straight forward. That being said, this method requires extra logic on the frontend of your website, which may not always be clean/necessary.

Another method, although certainly less common, is to assign an image as the featured image when an entry is saved from within WordPress. While this may seem like an acceptable alternative, what happens if the assigned image is, by mistake, removed from the WordPress “Media Library”? Alternatively, what is the theme changes and the assigned image is no longer relevant? With this method, you’d need to either run a fairly large database operation to swap out all the assigned post thumbnails for your new image, or change each manually. Either way, you’d need to do this for each change of the default image. Not ideal.

After reading a recent discussion in this regard, I decided to explore an alternative that would carry less impact on the maintenance of the system, as well as make it easy to change the default image and add as little extra logic to the template files in a WordPress theme as possible.

Enter the “Default WordPress Post Thumbnail” plugin.

How is this WordPress plugin different?

The “Default WordPress Post Thumbnail” plugin adds two settings to the “Settings > Media” screen in your WordPress installation: one to select which image in your “Media Library” is to be the default image, and another to specify whether or not to run the code on thumbnails displayed within the WordPress admin (note: not within the “Featured Image” meta box, but if an image is, for example, displayed in the posts list table).

From there, once a default image has been selected, it’s business as usual in your WordPress theme. No template file modifications are required. If you’re WordPress theme supports native WordPress post thumbnails and uses the the_post_thumbnail() function, you’re good to go.

The technical details, please?

The plugin checks if HTML has been generated for the post thumbnail of the given entry. If no HTML has been generated, it is assumed that no post thumbnail is present, at which point the code will generate the post thumbnail based on the default image specified. All parameters, sizes, etc are carried over from the original call to the_post_thumbnail(), which makes integrating your default image a breeze.

Change your theme and want a different default image? No problem. Just change the setting on the “Settings > Media” screen.

For me, this method carries less impact on the system, easier maintenance and updates, and requires no additional logic to be added to your WordPress theme files.

To use the plugin, it is available for download on GitHub.

What are your thoughts on this? 🙂

To catch up if you’re not entirely sure what a “post thumbnail” is, WordPress has functionality bundled into it’s core that allows a blogger/website manager to assign a specific image to be the “featured image” (or “post thumbnail”) for each page, blog post or entry to a custom post type.

Leave a Reply

  1. I’ve always been in favour of omitting post thumbs completely if a user doesn’t specify one per post. It obviously depends highly on the layout of the theme and how multi-post pages display, but a default image just looks tacky.

    • Thanks for your comment, Ashraf. 🙂

      In general, I agree that if no thumbnail is set, nothing should display. In some contexts (for example, a storefront), one may want to display a “not available” or “coming soon” image by default.

      The post was written with the scenario that the use case would require a default thumbnail (in response to several posts written within the WordPress community). The code is a proof of concept and an alternative for users who require a post thumbnail and don’t want to code it in manually, or save a default image to the database for each entry. 🙂