How to add “srcset” and “sizes” attributes to images in sidebar?

Is there a way to add srcset and sizes attributes to images in the sidebar?

I know we can use the following hooks for images in posts/pages and featured images:

wp_calculate_image_sizes
wp_get_attachment_image_attributes

But I’m not sure if there’s a way to set the srcset and sizes attributes to images in the sidebar, for example those uploaded via the Media Widget.

To clarify, this is a simplified example of what I’m using for content images:

function my_image_sizes_attr( $sizes, $size ) {

$width = $size[0];

if ( $width >= 760 ) {
return ‘(min-width: 769px) 760px, calc(100vw – 60px)’;
} else {
return $sizes;
}

}
add_filter( ‘wp_calculate_image_sizes’, ‘my_image_sizes_attr’, 10 , 2 );

Just curious if the same can be done for sidebar images.

Read more here:: How to add “srcset” and “sizes” attributes to images in sidebar?

Leave a Reply

Your email address will not be published. Required fields are marked *