Skip to content

Instantly share code, notes, and snippets.

@rinatkhaziev
Created September 21, 2015 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rinatkhaziev/53269a542425b1c816fa to your computer and use it in GitHub Desktop.
Save rinatkhaziev/53269a542425b1c816fa to your computer and use it in GitHub Desktop.
Display placeholder image when there's no thumbnail for the post
<?php
/**
* Filter to put a placeholder in, when there's no thumbnail set for post (which is the case for a lot of legacy posts)
*/
add_filter( 'post_thumbnail_html', function( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
// We have a thumb, return it
if ( $html )
return $html;
$placeholder_url = 'http://full.url.to/placeholder_image';
$attr = wp_parse_args( $attr, [ 'class' => '' ] );
return sprintf( '<img src="%s" class="%s" title="%s">',
esc_url( $placeholder_url ),
esc_attr( $attr[ 'class' ] ),
esc_attr( get_the_title( $post_id ) ) );
}, 10, 5 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment