How to add width, height, class, sizes and srcset attributes to images
-
Hello everyone, everything good?
I have an old website where the images were posted without the
class
,width
andheight
attributes. Because of this, WordPress does not automatically turn them into responsive images (usingsrcset
andsizes
).Would anyone know a way to add the
height
,width
,class
,srcset
andsizes
attributes to these images?To explain better …
Currently the images look like this:
<img src="https://www.example.com/blog/wp-content/uploads/2021/03/exampleImage.jpg">
I would like to leave it like this:
<img src="https://www.example.com/blog/wp-content/uploads/2021/03/exampleImage.jpg" width="720" height="450" class='example' srcset="https://www.example.com/blog/wp-content/uploads/2021/03/exampleImage-720x250.jpg 720w, https://www.example.com/blog/wp-content/uploads/2021/03/exampleImage-543x283.jpg 543w, https://www.example.com/blog/wp-content/uploads/2021/03/exampleImage-360x200.jpg 360w, https://www.example.com/blog/wp-content/uploads/2021/03/exampleImage-280x185.jpg 280w" sizes="(min-width: 861px) 720px, (max-width: 860px) 50vw, 100vw">
I tried to use the filter
wp_calculate_image_srcset
andwp_calculate_image_sizes
, however I was not successful, as it seems that these filters are only activated when the image already has thesrcset
andsizes
attributes.Note: I have no way to manually reload all images, as the blog has many posts.
- The topic ‘How to add width, height, class, sizes and srcset attributes to images’ is closed to new replies.