Support » Developing with WordPress » Customize image “srcset” and “sizes”

  • Resolved Gustavo Spindola

    (@gustavospindola)


    Hello everyone, everything good?

    I’m looking for a way to customize the “srcset” and “sizes” attributes of all the images in my posts (the ones that have already been posted, and the ones that will be posted), however, I’m only managing to modify the “srcset” attribute by adding the following code to the functions.php file:

    function custom_image_srcset_and_sizes($sources, $size_array, $image_src, $image_meta, $attachment_id) {
    
        $aux = explode('/', $image_meta['file']);
        //fictitious link
        $link = 'https://www.exemple.com/blog/wp-content/uploads/' . $aux[0] . '/' . $aux[1] . '/';
        //opening the new "srcset"
        $sources['720'] = array(
            'url' => $link . '' . $image_meta['sizes']['custom-size1']['file'],
            'descriptor' => 'w',
            'value' => '720',
        );
        $sources['543'] = array(
            'url' => $link . '' . $image_meta['sizes']['custom-size1']['file'],
            'descriptor' => 'w',
            'value' => '543',
        );
        $sources['360'] = array(
            'url' => $link . '' . $image_meta['sizes']['custom-size1']['file'],
            'descriptor' => 'w',
            'value' => '360',
        );
        $sources['280'] = array(
            'url' => $link . '' . $image_meta['sizes']['custom-size1']['file'],
            'descriptor' => 'w',
            'value' => '280',
        );
    
        return $sources;
    }
    
    add_filter('wp_calculate_image_srcset', 'custom_image_srcset_and_sizes', 10, 5);

    Could someone tell me how do I modify the “sizes” attribute (there are not the “height” and “width” attributes, is it really the “sizes” attribute)?

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

    (@joyously)

    Moderator bcworkz

    (@bcworkz)

    is it really the “sizes” attribute?

    Related, yet different than width and height attributes. The “sizes” attribute is used in tandem with “srcset” to help a browser determine the optimal source image file based on media type/size. It’s a corollary to CSS media queries.
    https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Img#attr-sizes

    Thread Starter Gustavo Spindola

    (@gustavospindola)

    @joyously

    I tried to use the wp_image_add_srcset_and_sizes function, but I couldn’t.
    Could you give me an example please?

    To explain a little better what I would like.
    Wordpress produces the following image

    <img loading="lazy" 
    	src="https://www.exemple.com/blog/wp-content/uploads/2021/03/exempleImage.jpg" 
    	width="720" height="450" 
    	srcset="https://www.exemple.com/blog/wp-content/uploads/2021/03/exempleImage.jpg 720w, 
    	https://www.exemple.com/blog/wp-content/uploads/2021/03/exempleImage-300x188.jpg 300w, 
    	https://www.exemple.com/blog/wp-content/uploads/2021/03/exempleImage-528x330.jpg 528w, 
    	https://www.exemple.com/blog/wp-content/uploads/2021/03/exempleImage-98x60.jpg 98w" 
    	sizes="(max-width: 720px) 100vw, 720px">

    I would like to leave it this way

    <img loading="lazy"  
    	src="https://www.exemple.com/blog/wp-content/uploads/2021/03/exempleImage-720x250.jpg" 
    	width="720" height="450"
    	srcset="https://www.exemple.com/blog/wp-content/uploads/2021/03/exempleImage-720x250.jpg 720w,
    	https://www.exemple.com/blog/wp-content/uploads/2021/03/exempleImage-543x283.jpg 543w,
    	https://www.exemple.com/blog/wp-content/uploads/2021/03/exempleImage-360x200.jpg 360w,
    	https://www.exemple.com/blog/wp-content/uploads/2021/03/exempleImage-280x185.jpg 280w" 
    	sizes="(min-width: 861px) 720px, (max-width: 860px) 50vw, 100vw">
    Joy

    (@joyously)

    WP will already call wp_image_add_srcset_and_sizes, so you don’t need to. That function will call the other two functions, and you can add a filter for both.

    Thread Starter Gustavo Spindola

    (@gustavospindola)

    Sorry @joyously , but I didn’t understand 🙁

    I tried to use the function wp_image_add_srcset_and_sizes, but I couldn’t

    I don’t know how to program very well for wordpress
    Could you give me a practical example please?

    Joy

    (@joyously)

    Just like your original code has
    add_filter('wp_calculate_image_srcset', 'custom_image_srcset_and_sizes', 10, 5);
    you can add more like

    add_filter('wp_calculate_image_sizes', 'custom_image_sizes', 10, 5);
    function custom_image_sizes($sizes, $size, $image_src, $image_meta, $attachment_id) {
    //custom code here...
      return $sizes;
    }
    Thread Starter Gustavo Spindola

    (@gustavospindola)

    It looks like it worked @joyously
    Thanks a lot for the help

    Way to solve the problem

    function custom_image_srcset_and_sizes($sources, $size_array, $image_src, $image_meta, $attachment_id) {
    
        $aux = explode('/', $image_meta['file']);
        //fictitious link
        $link = 'https://www.exemple.com/blog/wp-content/uploads/' . $aux[0] . '/' . $aux[1] . '/';
        //opening the new "srcset"
        $sources['720'] = array(
            'url' => $link . '' . $image_meta['sizes']['custom-size1']['file'],
            'descriptor' => 'w',
            'value' => '720',
        );
        $sources['543'] = array(
            'url' => $link . '' . $image_meta['sizes']['custom-size1']['file'],
            'descriptor' => 'w',
            'value' => '543',
        );
        $sources['360'] = array(
            'url' => $link . '' . $image_meta['sizes']['custom-size1']['file'],
            'descriptor' => 'w',
            'value' => '360',
        );
        $sources['280'] = array(
            'url' => $link . '' . $image_meta['sizes']['custom-size1']['file'],
            'descriptor' => 'w',
            'value' => '280',
        );
    
        return $sources;
    }
    
    add_filter('wp_calculate_image_srcset', 'custom_image_srcset_and_sizes', 10, 5);
    
    function custom_image_sizes($sizes) {
        return "(min-width: 861px) 720px, (max-width: 860px) 50vw, 100vw";
    }
    add_filter('wp_calculate_image_sizes', 'custom_image_sizes', 10, 1);
    
Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.