Default Featured Image
Uses an image of your choice as the featured image for posts that do not have one, so archive grids, related posts and social shares never show a gap.
Opens WPIDE on
Code
// Attachment ID of the fallback image.
$wpide_default_thumbnail_id = 0;
// Post types that use the fallback.
$wpide_default_thumbnail_types = array( 'post' );
add_filter( 'post_thumbnail_id', function ( $thumbnail_id, $post ) use ( $wpide_default_thumbnail_id, $wpide_default_thumbnail_types ) {
if ( $thumbnail_id || ! $wpide_default_thumbnail_id || is_admin() || ! $post ) {
return $thumbnail_id;
}
return in_array( get_post_type( $post ), $wpide_default_thumbnail_types, true ) ? (int) $wpide_default_thumbnail_id : $thumbnail_id;
}, 10, 2 );
Notes
Upload the fallback image, copy its attachment ID from the Media Library and put it in $wpide_default_thumbnail_id.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.