WordPress: How to set a default image as a fallback for missing featured images

In today's part of the series of articles about featured images, I have a little plugin tip for you with which you can configure a standard image in WordPress, which will be displayed if the featured images are missing. This fallback ensures that every post in WordPress is always assigned a post image.

You cannot always find a suitable picture for the new article. It is usually better to select a standard image than to have no image at all. Finally, featured images often need to be available for social sharing via Facebook and Twitter.

Sometimes it is simply necessary that a contribution image exists for all contributions. For example, if the theme uses a two-column grid layout for the blog and missing featured images lead to large gaps in the layout.

So that you don't always have to manually assign the same standard image and the featured image is not forgotten, you can easily set a certain image as the default with the Default Featured Image Plugin .

Default featured image plugin

You can download the plugin from the WordPress.org plugin directory:

It only serves one purpose without a lot of fuss and works perfectly with WordPress 4.9.1, even if currently only compatibility up to 4.8.4 is specified.

Configure WordPress standard post image

The plugin adds a new option for configuring the default post image in the WordPress backend under Settings → Media .

Upload WordPress standard post image

A new picture can be uploaded there or an existing picture can be selected from the media library.

WordPress Default Featured Image Options

The selected image is then displayed as fallback if no featured image has been set. In addition to the very simple basic functionality, the plugin can also be adapted with a filter.

Adjust plugin with filter

By default, the plugin shows the standard image everywhere, ie for all posts, static pages and custom post types.

With the dfi_thumbnail_id filter this behavior can be adjusted. For example, if the standard featured image is only to be used for blog posts, but not for static pages and other post types, this snippet of code can be used:

 function theme_slug_default_featured_image( $dfi_thumbnail_id, $post_id ) { $post = get_post( $post_id ); // Show default featured image only on blog posts if ( 'post' === $post->post_type ) { return $dfi_thumbnail_id; } // Return invalid Image ID return 0; } add_filter( 'dfi_thumbnail_id', 'theme_slug_default_featured_image', 10, 2 );

Alternatively, the fallback for the featured image can only be used for single-view articles. Basically, there are a lot of options available when using WordPress conditional tags .

 function theme_slug_default_featured_image( $dfi_thumbnail_id, $post_id ) { // Show default featured image only on single posts if ( is_single() ) { return $dfi_thumbnail_id; } // Return invalid Image ID return 0; } add_filter( 'dfi_thumbnail_id', 'theme_slug_default_featured_image', 10, 2 );

You can not only use the filter to restrict the display of the standard image on certain pages, but you can also set several standard images, for example.

With this code Schipsel, the Books category receives a standard image with the ID 453, the WordPress category the image 2321. Otherwise, the normal standard image that was selected in the media settings is used.

 function theme_slug_default_featured_image( $dfi_thumbnail_id, $post_id ) { // Show different default image for category Books if ( has_category( 'books', $post_id ) ) { return 453; // Default Books Image ID } // Show different default image for category WordPress if ( has_category( 'wordpress', $post_id ) ) { return 2321; // Default WordPress Image ID } // Return default featured image return $dfi_thumbnail_id; } add_filter( 'dfi_thumbnail_id', 'theme_slug_default_featured_image', 10, 2 );

The code snippets can be inserted into the functions.php of a child theme or with the Code Snippets plugin .

Overview of all articles in this series