apply_filters( 'max_srcset_image_width', int $max_width, array $size_array )

Filters the maximum image width to be included in a ‘srcset’ attribute.


Description Description


Parameters Parameters

$max_width

(int) The maximum image width to be included in the 'srcset'. Default '2048'.

$size_array

(array) Array of width and height values in pixels (in that order).


Top ↑

Source Source

File: wp-includes/media.php

View on Trac


Top ↑

Changelog Changelog

Changelog
Version Description
4.4.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 2 content
    Contributed by Anthony Hortin

    If you have the $content_width variable set (which is mandatory for themes in the .org Theme Directory), then this width overrides the max_srcset_image_width. Using @joemcgill’s example above to remove the default width wont work if you have $content_width set to a lower value.

  2. Skip to note 3 content
    Contributed by Joe McGill

    Increase the limit based on the size of the original.

    In this example, we’re going to change the maximum allowed image width to 2048px if the original image is wider than 800px.

    function custom_max_srcset_image_width( $max_width, $size_array ) {
        $width = $size_array[0];
    
        if ( $width > 800 ) {
            $max_width = 2048;
        }
    
        return $max_width;
    }
    add_filter( 'max_srcset_image_width', 'custom_max_srcset_image_width', 10, 2 );
  3. Skip to note 4 content
    Contributed by vitamindoug

    Hey Joe, your notes were a big help! Just for clarification though, what is the $max_width variable? When using this it appears to be looking at screen size, similar to a media query. For example: if screen is wider than 800px, load in the largest registered image size that’s not wider than 2048px. I’m a little confused about the “if the original image is wider than 800px” part. How does the original image size factor into what image is chosen?

You must log in before being able to contribute a note or feedback.