-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Auto Sizes for Lazy-loaded Images #7253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
909b731
ea207f3
dac231d
f0d949c
1efb4e6
c7992a0
deec3ba
cb44489
e4a141a
67b7882
fedaf73
639c086
ce31c1a
d7f497c
751dae6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1137,6 +1137,16 @@ function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = f | |
| } | ||
| } | ||
|
|
||
| // Adds 'auto' to the sizes attribute if applicable. | ||
| if ( | ||
| isset( $attr['loading'] ) && | ||
| 'lazy' === $attr['loading'] && | ||
| isset( $attr['sizes'] ) && | ||
| ! wp_sizes_attribute_includes_valid_auto( $attr['sizes'] ) | ||
| ) { | ||
| $attr['sizes'] = 'auto, ' . $attr['sizes']; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this remove
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Do any of the implementations respect it in spite of the spec if the CSS is available and the image isn't set for lazy loading? If so then I think it would be good to keep it given the sizes attributes can be inaccurate in many situations and the CSS may be in the HTML header and therefore available.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@westonruter Core only adds the "auto" in sizes when image is lazy-loaded so if user added that through filter then core will not remove "auto". @peterwilsoncc Even if the CSS is available early in the document (such as in the HTML header), implementations do not dynamically adjust the sizes attribute based on CSS unless lazy loading is enabled.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is needed as Core doesn't add Now I'm not strongly against adding this, but it's not a requirement for this feature. We could discuss whether this is worth adding in a separate ticket, or wait if it comes up as a real use-case problem somewhere. |
||
| } | ||
|
|
||
| /** | ||
| * Filters the list of attachment image attributes. | ||
| * | ||
|
|
@@ -1917,6 +1927,9 @@ function wp_filter_content_tags( $content, $context = null ) { | |
| // Add loading optimization attributes if applicable. | ||
| $filtered_image = wp_img_tag_add_loading_optimization_attrs( $filtered_image, $context ); | ||
|
|
||
| // Adds 'auto' to the sizes attribute if applicable. | ||
| $filtered_image = wp_img_tag_add_auto_sizes( $filtered_image ); | ||
|
|
||
| /** | ||
| * Filters an img tag within the content for a given context. | ||
| * | ||
|
|
@@ -1963,6 +1976,59 @@ function wp_filter_content_tags( $content, $context = null ) { | |
| return $content; | ||
| } | ||
|
|
||
| /** | ||
| * Adds 'auto' to the sizes attribute to the image, if the image is lazy loaded and does not already include it. | ||
| * | ||
| * @since 6.7.0 | ||
| * | ||
| * @param string $image The image tag markup being filtered. | ||
| * @return string The filtered image tag markup. | ||
| */ | ||
| function wp_img_tag_add_auto_sizes( string $image ): string { | ||
| $processor = new WP_HTML_Tag_Processor( $image ); | ||
|
|
||
| // Bail if there is no IMG tag. | ||
| if ( ! $processor->next_tag( array( 'tag_name' => 'IMG' ) ) ) { | ||
| return $image; | ||
| } | ||
|
|
||
| // Bail early if the image is not lazy-loaded. | ||
| $value = $processor->get_attribute( 'loading' ); | ||
| if ( ! is_string( $value ) || 'lazy' !== strtolower( trim( $value, " \t\f\r\n" ) ) ) { | ||
| return $image; | ||
| } | ||
|
|
||
| $sizes = $processor->get_attribute( 'sizes' ); | ||
|
|
||
| // Bail early if the image is not responsive. | ||
| if ( ! is_string( $sizes ) ) { | ||
| return $image; | ||
| } | ||
|
|
||
| // Don't add 'auto' to the sizes attribute if it already exists. | ||
| if ( wp_sizes_attribute_includes_valid_auto( $sizes ) ) { | ||
| return $image; | ||
| } | ||
|
|
||
| $processor->set_attribute( 'sizes', "auto, $sizes" ); | ||
| return $processor->get_updated_html(); | ||
| } | ||
|
|
||
| /** | ||
| * Checks whether the given 'sizes' attribute includes the 'auto' keyword as the first item in the list. | ||
| * | ||
| * Per the HTML spec, if present it must be the first entry. | ||
| * | ||
| * @since 6.7.0 | ||
| * | ||
| * @param string $sizes_attr The 'sizes' attribute value. | ||
| * @return bool True if the 'auto' keyword is present, false otherwise. | ||
| */ | ||
| function wp_sizes_attribute_includes_valid_auto( string $sizes_attr ): bool { | ||
| list( $first_size ) = explode( ',', $sizes_attr, 2 ); | ||
| return 'auto' === strtolower( trim( $first_size, " \t\f\r\n" ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Adds optimization attributes to an `img` HTML tag. | ||
| * | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.