apply_filters( ‘post_thumbnail_html’, string $html, int $post_id, int $post_thumbnail_id, string|int[] $size, string|array $attr )

Filters the post thumbnail HTML.

Parameters

$htmlstring
The post thumbnail HTML.
$post_idint
The post ID.
$post_thumbnail_idint
The post thumbnail ID, or 0 if there isn’t one.
$sizestring|int[]
Requested image size. Can be any registered image size name, or an array of width and height values in pixels (in that order).
$attrstring|array
Query string or array of attributes.

Source

return apply_filters( 'post_thumbnail_html', $html, $post->ID, $post_thumbnail_id, $size, $attr );

Changelog

VersionDescription
2.9.0Introduced.

User Contributed Notes

  1. Skip to note 3 content

    As of WP 5.9 (as far as I can tell), the $attr parameter indicates it accepts a string or array. If you are using parameter-typing in your methods/functions, and you previously indicated that your function should expect a string, you will see fatal errors due to this (as arrays are also sent, now). The change appears to have occurred here.

  2. Skip to note 4 content

    Add title attribute to post thumbnail:

    function wpdocs_addTitleToThumbnail( $html ) {
        $id = get_post_thumbnail_id();
        $alt_text = get_post_meta( $id, '_wp_attachment_image_alt', true );
    
        $html = str_replace( 'alt=', 'title="' . esc_attr( $alt_text ) . '" alt=', $html );
    
        return $html;
    }
    add_filter( 'post_thumbnail_html', 'wpdocs_addTitleToThumbnail' );

You must log in before being able to contribute a note or feedback.