The end_fetch_post_thumbnail_html WordPress action fires after fetching the post thumbnail HTML.
Usage
add_action('end_fetch_post_thumbnail_html', 'your_custom_function', 10, 3); function your_custom_function($post_id, $post_thumbnail_id, $size) { // your custom code here }
Parameters
- $post_id (int): The post ID.
- $post_thumbnail_id (int): The post thumbnail ID.
- $size (string|int[]): Requested image size. Can be any registered image size name or an array of width and height values in pixels (in that order).
More information
See WordPress Developer Resources: end_fetch_post_thumbnail_html
Examples
Add a watermark to post thumbnail
Add a watermark to the post thumbnail after it’s fetched.
add_action('end_fetch_post_thumbnail_html', 'add_watermark_to_thumbnail', 10, 3); function add_watermark_to_thumbnail($post_id, $post_thumbnail_id, $size) { // Get the image source $image_src = wp_get_attachment_image_src($post_thumbnail_id, $size); // Add watermark to the image $watermarked_image_src = add_watermark($image_src[0]); // Replace the original image src with the watermarked image src echo str_replace($image_src[0], $watermarked_image_src, get_the_post_thumbnail($post_id, $size)); }
Add custom CSS class to post thumbnail
Add a custom CSS class to the post thumbnail HTML.
add_action('end_fetch_post_thumbnail_html', 'add_custom_css_class_to_thumbnail', 10, 3); function add_custom_css_class_to_thumbnail($post_id, $post_thumbnail_id, $size) { // Add custom CSS class to the thumbnail $class = 'custom-thumbnail-class'; echo str_replace('class="', 'class="' . $class . ' ', get_the_post_thumbnail($post_id, $size)); }
Add data attribute to post thumbnail
Add a data attribute to the post thumbnail HTML.
add_action('end_fetch_post_thumbnail_html', 'add_data_attribute_to_thumbnail', 10, 3); function add_data_attribute_to_thumbnail($post_id, $post_thumbnail_id, $size) { // Add data attribute to the thumbnail $data_attribute = 'data-custom="example"'; echo str_replace('<img', '<img ' . $data_attribute, get_the_post_thumbnail($post_id, $size)); }
Add lazy loading to post thumbnails
Add lazy loading attribute to post thumbnails.
add_action('end_fetch_post_thumbnail_html', 'add_lazy_loading_to_thumbnail', 10, 3); function add_lazy_loading_to_thumbnail($post_id, $post_thumbnail_id, $size) { // Add lazy loading attribute to the thumbnail echo str_replace('<img', '<img loading="lazy"', get_the_post_thumbnail($post_id, $size)); }
Add image dimensions to the post thumbnail
Add width and height attributes to the post thumbnail.
add_action('end_fetch_post_thumbnail_html', 'add_dimensions_to_thumbnail', 10, 3); function add_dimensions_to_thumbnail($post_id, $post_thumbnail_id, $size) { // Get the image dimensions $image_src = wp_get_attachment_image_src($post_thumbnail_id, $size); $width = $image_src[1]; $height = $image_src[2]; // Add width and height attributes to the thumbnail $thumbnail_html = get_the_post_thumbnail($post_id, $size); $thumbnail_html = str_replace('<img', '<img width="' . $width . '" height="' . $height . '"', $thumbnail_html); echo $thumbnail_html; }