Support » Developing with WordPress » How to add width, height, class, sizes and srcset attributes to images

  • Gustavo Spindola

    (@gustavospindola)


    Hello everyone, everything good?

    I have an old website where the images were posted without the class, width and height attributes. Because of this, WordPress does not automatically turn them into responsive images (using srcset and sizes).

    Would anyone know a way to add the height, width, class, srcset and sizes 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 and wp_calculate_image_sizes, however I was not successful, as it seems that these filters are only activated when the image already has the srcset and sizes attributes.

    Note: I have no way to manually reload all images, as the blog has many posts.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Joy

    (@joyously)

    You don’t need to add the srcset and sizes attributes. If you only added the wp-image-xxx class, then WP will add the srcset and sizes. It uses the image ID in the class name to find the image information to use. It’s best done dynamically, since you can switch themes and change your mind about image sizes. Also, as you noted, it can be filtered.

    I thought that perhaps the Regenerate Thumbnails plugin might put the class name on the img tag, but I just tested it and it does not. But there might be some other plugin that does similar processing. You might need to tweak a plugin’s code to add the class name and then you’ll be all set.

    Thread Starter Gustavo Spindola

    (@gustavospindola)

    Thanks for helping me again @joyously .

    I believe I got a solution, but I don’t know if it is the correct way to do this and if there is any impact on the website’s performance.
    Can you tell me if what I did is wrong to do?
    The code is this

    function custom_image($content) {
        $content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8");
        $document = new \DOMDocument();
        libxml_use_internal_errors(true);
        $document->loadHTML(utf8_decode($content));
        $images = $document->getElementsByTagName('img');
        foreach ($images as $image) {
            if ($image->getAttribute('srcset') == '') {
                $aux = explode('.jpg', $image->getAttribute('src'));
                $srcset = "$aux[0]-720x250.jpg 720w, "
                        . "$aux[0]-543x283.jpg 543w, "
                        . "$aux[0]-360x200.jpg 360w, "
                        . "$aux[0]-280x185.jpg 280w";
                $image->setAttribute('srcset', $srcset);
                $image->setAttribute('sizes', '(min-width: 861px) 720px, (max-width: 860px) 50vw, 100vw');
                $image->setAttribute('height', '330');
                $image->setAttribute('width', '720');
                $image->setAttribute('class', 'aligncenter lazy');
            }
        }
        return $document->saveHTML();
    }
    
    add_filter('the_content', 'custom_image');

    Comments:
    1 – I’m adding the srcset and sizes attributes in a personalized way, because I didn’t like the default values of wordpress (anyone who wants another way to customize the srcset and sizes attributes can find it in this other topic).
    2 – The code is limited to .jpg images only, but as it is a prototype, this is not a problem at the moment

    Joy

    (@joyously)

    I was thinking more along the lines of fixing your content once, and then filtering the srcset if you felt you needed to. Surely you don’t have all images the exact same size. If you use the name to look up the image ID, then add the wp-image-xxx class, it’s a lot less fragile.

    I tried your code, and there is a problem. The output has the <html> and <body> tag wrappers on it.
    Setting the class like that overrides any existing class.
    Do you have a plugin to do something with the lazy class? If you want the WP lazy loading, which can be filtered, use the wp-image-xxx class. (the attribute is loading="lazy")
    WP also adds the width and height, if the wp-image-xxx class is there.

    Thread Starter Gustavo Spindola

    (@gustavospindola)

    I understood
    Is there a way to retrieve the image ID via the URL?
    Because, this way I would be able to accomplish what you said
    Adding the ID to the image, WordPress would in theory put the srcset and sizes automatically and with that I would customize the srcset and sizes through a filter.

    Sorry, but I didn’t understand the problem related to html and body tags

    Class lazy is not being used with a plugin. I use it with a javascript that performs lazyload.
    If possible, I would like to use the WordPress native lazyload, but as I don’t have the wp-image-xxx class in the images, it doesn’t do that.

    I have an idea!
    Are the structure of the images saved in the correct database? I could create a script to access the database and modify the images.
    Can you tell me in which database table the images are saved?

    Joy

    (@joyously)

    If your images were uploaded through WP, they are saved in the posts table, with the file details in postmeta table. But if they were, they would have all the correct classes on them (unless you manually removed them as you added the images).
    If your images are not in the database, you can use Import Legacy Media plugin or Add From Server plugin. There are likely newer ones also. But you could modify the plugin so that as it adds the image, it could update the class where it is used in content. Or just do it separately…

    This function helps you find the image:
    https://developer.wordpress.org/reference/functions/wp_image_file_matches_image_meta/

    Thread Starter Gustavo Spindola

    (@gustavospindola)

    That’s exactly what happened
    The images were posted on WordPress, and manually, the classes were removed

    I was analyzing these two tables to try to manipulate them. But I believe that what I thought will not work

    I’ll look at this function that you gave me
    Thanks a lot for the help

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.