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 functions.php file. It will set a default featured image if the user does not put it for a post or page. How to create a WordPress child theme and how to edit functions.php on WordPress themes blog posts will help you if you do not know about each.

/* 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.

Or else, you can edit the path or file name/extension as you wish. 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 below screenshot is from the same website as above. The post with the title “Explore WordPress 6.4” has a placeholder now.

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:

Author:

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 *