Adding A Default Featured Image Or Thumbnail In WordPress

Adding A Default Featured Image Or Thumbnail In WordPress

On this site, any post without a featured image specifically set (such as this one) displays the default floral ‘K’ insignia anywhere on the site where a thumbnail is displayed. I looked around the web and none of the solutions I found worked. I eventually figured out that adding the following to my theme’s functions.php file did the trick:


/**
* Default post thumbnail image.
*
*/

function mk_filter_thumbnail_id( $thumbnail_id, $post = null ) {
if ( ! $thumbnail_id ) {
$thumbnail_id = 289; //id of default featured image
}

    return $thumbnail_id;
}

if (! is_admin() ) { //don't do this on admin pages, it sometimes overwrites thumbnails in the back-end view of the media library
add_filter( 'post_thumbnail_id', 'mk_filter_thumbnail_id', 20, 5 );
}

By the way, I still haven’t grasped what the third and fourth parameters of add_filter() do… I copied them out of a Stack Overflow answer and they worked. WordPress’s whole “filter” functionality is an odd way of calling functions, although I suppose it’s a practical if roundabout way of hooking into existing functions to add your own code modifications.