2

I want WordPress to return the first post image or a default image if no featured image has been set. My theme uses the_post_thumbnail many times, so I don't want to go through and change all references to a new function. I'd rather filter the core. Here's what I've added to functions.php:

add_filter('post_thumbnail_html', 'my_thumbnail_html', 10, 5);

function my_thumbnail_html( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
    global $post, $posts;
    if (has_post_thumbnail() ) {
        echo get_the_post_thumbnail( null, $size, $attr );
    }
    else {
        $first_img = '';
        ob_start();
        ob_end_clean();
        $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
        $first_img = $matches [1] [0];
        if(empty($first_img)){
            $first_img = get_bloginfo("template_url") . '/images/default.gif';
        }
        return $first_img;
    }
}

How do I hook this correctly?

3
  • what exactly you want to do ?
    – Amit Kosti
    Aug 2, 2012 at 10:43
  • Set priorty higher the 10 and the number of arguments Set to 3
    – Bainternet
    Aug 2, 2012 at 10:59
  • I've clarified my ultimate goal and filled in the function thus far.
    – Zade
    Aug 2, 2012 at 12:22

2 Answers 2

4

Actually there isn't the_post_thumbnail filter applied anywhere.

The one you could try to use and to modify content that goes to the_post_thumbnail function is the post_thumbnail_html filter what is called with the following arguments $html, $post_id, $post_thumbnail_id, $size, $attr.

2
  • Thanks for the guidance. I've changed the filter to reference post_thumbnail_html, thought I don't know if I'm hooking it in properly. I'll have more time to look at this after sleep...
    – Zade
    Aug 2, 2012 at 12:26
  • I think I'm in over my head...I notice return apply_filters( 'post_thumbnail_html', $html, $post_id, $post_thumbnail_id, $size, $attr ); within the get_the_post_thumbnail function in wp-includes/post-thumbnail-template.php, but I don't understand how to modify that using my code above...which now prevents the site from fully loading a page. If it's too complicated I'll just change my theme files one by one.
    – Zade
    Aug 11, 2012 at 6:37
1

Figured it out:

function get_attachment_id_from_src( $image_src ) {
    global $wpdb;
    $query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$image_src'";
    $id = $wpdb->get_var($query);
    return $id;
}

add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 5 );

function my_post_image_html( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
    if( '' == $html ) {
        global $post, $posts;
        ob_start();
        ob_end_clean();
        $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
        $first_img = $matches [1] [0];
        if ( empty( $first_img ) ){
            $image_id = 129; // default image ID
        }
        else {
            $image_id = get_attachment_id_from_src($first_img);
        }
        $html = wp_get_attachment_image( $image_id, $size, false, $attr );
    }
    return $html;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.