How to Set a Default Featured Image in WordPress

WordPress does not have a default featured image for posts or pages. It shows nothing as a placeholder. See the picture below of a post with the title “Explore WordPress 6.4” that does not have a featured image.

Add Some Codes to Set a Default Featured Image

Add the below codes to your theme or, if you are not the theme developer, your child theme’s functions.php file. If the user does not put it in a post or page, it will set a default featured image. If you do not know how to create a WordPress child theme or how to edit functions.php on WordPress themes, these blog posts will help you.

/* Setup a default post featured image */
function alvand_filter_post_thumbnail_html($html)
{
    // If there is no post thumbnail, return a default image
    if ('' == $html) {
        return '<img src="' . get_stylesheet_directory_uri() . '/assets/images/default-post-thumbnail.webp" alt="default post thumbnail - a mountain and sun" />';
    }
    // Else, return the post thumbnail
    return $html;
}
add_filter('post_thumbnail_html', 'alvand_filter_post_thumbnail_html');

Modifications needed to Default Featured Image Codes

You should consider where the default featured image is stored in the code.

I declared to use an image named default-post-thumbnail.webp in the theme or child theme root directory > assets > images. It is a relative path. You can make folders (if they do not exist) and put a file with the mentioned name and extension (.webp) in the right place.

You can also edit the path or file name/extension. In this case, be careful not to change anything wrongly. One single quote more or less can make the codes not work.

You can also change the alt text from default post thumbnail - a mountain and sun to any text you want.

See the result

As a result, the screenshot below is from the same website as the above. The post titled “Explore WordPress 6.4” now has a placeholder.

Conclusion

In the end, I hope this short blog post will help you with the task successfully. You may also interested in How to Change the 404 Page Title in WordPress so don’t miss it.

Published on:

Updated on:

Rate this post and help us improve our content to better serve you.

Help us spread the word! Click the share buttons below to share this post with your followers.

We’d love to hear your thoughts on this topic. Feel free to leave a comment below.

Leave a Reply

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

4 + 2 =