How to Set the_post_thumbnail as Default in WordPress

Home » Snippets » How to Set the_post_thumbnail as Default in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Struggling with setting a default the_post_thumbnail on your WordPress site? Thumbnails are a powerful feature of WordPress, reflecting the post’s content in a graphical way and enhancing the overall aesthetic. However, when a thumbnail is not set, it can tamper the uniformity of your design. In this article, we’ll show you how to set a default the_post_thumbnail in WordPress to maintain your site’s visual consistency and give it a more professional look, even when some posts lack a dedicated thumbnail.

					function wpturbo_default_post_thumbnail( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
    if ( empty( $html ) ) {
        $html = '<img src="'. get_template_directory_uri() . '/path_to_your_default_image.png" ' . $attr . '>';
    }
    return $html;
}
add_filter( 'post_thumbnail_html', 'wpturbo_default_post_thumbnail', 10, 5 );
				

The first step of this code snippet is defining the function wpturbo_default_post_thumbnail(). This function is going to check whether the analyzed post has a thumbnail. If it doesn’t, then the function will provide a default one.

The wpturbo_default_post_thumbnail function accepts five parameters:

  • $html: This is usually the HTML content of the featured image.
  • $post_id: This is the ID of the post that the function will check.
  • $post_thumbnail_id: This is the ID of the thumbnail that is attached to the post.
  • $size: This is an array that specifies the width and height of the thumbnail.
  • $attr: An array of attributes applied on the image tag.
if ( empty( $html ) ) {
    $html = '<img src="'. get_template_directory_uri() . '/path_to_your_default_image.png" ' . $attr . '>';
}

Inside the function, an if statement is used to check if $html is empty, meaning there is no thumbnail image for the post. If it is indeed empty, the img tag gets created with a src attribute pointing towards the default image you specified.

The get_template_directory_uri() function retrieves the URI of the current theme directory which is then concatenated with the relative path to your default image.

The $attr parameter is added to the img tag to apply any additional attributes that you’d want to pass to the image.

Finally, if the post already has a thumbnail, the function will simply return the original $html content.

return $html;

At the end of the definition, we hook the wpturbo_default_post_thumbnail function to the post_thumbnail_html filter. The post_thumbnail_html filter allows you to modify the HTML of the post thumbnail, if it exists.

add_filter( 'post_thumbnail_html', 'wpturbo_default_post_thumbnail', 10, 5 );

The parameters used with add_filter() are:

  • Hook: post_thumbnail_html – the name of the filter to hook the function to.
  • Callable: wpturbo_default_post_thumbnail – the name of the function to be called when the filter is applied.
  • Priority: 10 – this is the order in which functions hooked to a particular filter are executed. Lower numbers correspond with earlier execution.
  • Accepted args: 5 – the number of arguments the function accepts.

All in all, this function allows you to set a default thumbnail for a given post, if it doesn’t have one set already. This can be particularly useful to ensure visual consistency in your WordPress site.

Register an account to save your snippets or go Pro to get more features.