Skip to content

Instantly share code, notes, and snippets.

@tcrsavage
Created July 17, 2013 02:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcrsavage/6017245 to your computer and use it in GitHub Desktop.
Save tcrsavage/6017245 to your computer and use it in GitHub Desktop.
Default the_post_thumbnail() and get_the_post_thumbnail() to return the first image in post content if no featured image is set
//If no post thumbnail exists, get the first image in the post and return that instead
add_filter( 'post_thumbnail_html', function( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
if ( $html )
return $html;
$found = preg_match( '/<img.+?class=\".+?([0-9]+).*\/>/', get_post( $post_id )->post_content, $matches );
if ( $found )
$html = wp_get_attachment_image( end( $matches ), $size, false, $attr );
return $html;
}, 10, 5 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment