Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Add filter example
add_filter('post_thumbnail_html', 'get_first_image_if_no_thumb');
function get_first_image_if_no_thumb($html, $post_id, $post_image_id) {
if ($html == '') {
$post = get_post($post_id);
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches[1][0];
if (isset($first_img)) {
return '<img width="300" class="attachment-medium size-medium wp-post-image" src='. $first_img .'></img>'
return $html;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment