From 5aa7a8ce78b685b3aae75837f9a49b1167232155 Mon Sep 17 00:00:00 2001 From: Andrew Ozz Date: Thu, 22 Oct 2015 19:30:12 +0000 Subject: [PATCH] Responsive Images: limit the size of images included in `srcset` attributes. Introduce `max_srcset_image_width` filter to adjust the limit. Props joemcgill. Fixes #34341. git-svn-id: https://develop.svn.wordpress.org/trunk@35358 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/media.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 210bf425c9..de1f2c480d 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -928,6 +928,15 @@ function wp_get_attachment_image_srcset_array( $attachment_id, $size = 'medium' */ $img_edited = preg_match( '/-e[0-9]{13}/', $img_url, $img_edit_hash ); + /** + * Filter the maximum width included in a srcset attribute. + * + * @since 4.4.0 + * + * @param array|string $size Size of image, either array or string. + */ + $max_srcset_width = apply_filters( 'max_srcset_image_width', 1600, $size ); + /* * Set up arrays to hold url candidates and matched image sources so * we can avoid duplicates without looping through the full sources array @@ -945,6 +954,11 @@ function wp_get_attachment_image_srcset_array( $attachment_id, $size = 'medium' continue; } + // Filter out images that are wider than $max_srcset_width. + if ( $max_srcset_width && $img['width'] > $max_srcset_width ) { + $contiue; + } + $candidate_url = path_join( dirname( $img_url ), $img['file'] ); // Calculate the new image ratio.