From 1d010a49c735b2d9f21e1d96e4f3b2eb032e591c Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Thu, 2 Jul 2020 02:03:03 +0000 Subject: [PATCH] Media: Improve support for opting out of lazy-loading for template images. With this changeset, in addition to the already present `wp_lazy_loading_enabled` filter, developers can now opt out of lazy-loading template images via `wp_get_attachment_image()` by passing a `loading` attribute with boolean value `false`. This can be used e.g. by theme developers on images which are very likely to be in the initial viewport. This changeset also improves related test coverage. Props adamsilverstein, azaozz, joemcgill, johnbillion. See #50425, #44427. Built from https://develop.svn.wordpress.org/trunk@48272 git-svn-id: http://core.svn.wordpress.org/trunk@48041 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/media.php | 13 ++++++++++--- wp-includes/version.php | 2 +- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/wp-includes/media.php b/wp-includes/media.php index 0dc67f7b86..74796210e2 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -1048,6 +1048,12 @@ function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = f $attr = wp_parse_args( $attr, $default_attr ); + // If `loading` attribute default of `lazy` is overridden for this + // image to omit the attribute, ensure it is not included. + if ( array_key_exists( 'loading', $attr ) && ! $attr['loading'] ) { + unset( $attr['loading'] ); + } + // Generate 'srcset' and 'sizes' if not already present. if ( empty( $attr['srcset'] ) ) { $image_meta = wp_get_attachment_metadata( $attachment_id ); @@ -1725,9 +1731,10 @@ function wp_img_tag_add_loading_attr( $image, $context ) { * * @since 5.5.0 * - * @param string $value The `loading` attribute value, defaults to `lazy`. - * @param string $image The HTML `img` tag to be filtered. - * @param string $context Additional context about how the function was called or where the img tag is. + * @param string|bool $value The `loading` attribute value. Returning a false-y value will result in the + * attribute being omitted for the image. Default is `lazy`. + * @param string $image The HTML `img` tag to be filtered. + * @param string $context Additional context about how the function was called or where the img tag is. */ $value = apply_filters( 'wp_img_tag_add_loading_attr', 'lazy', $image, $context ); diff --git a/wp-includes/version.php b/wp-includes/version.php index a8beabb915..d35e664540 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.5-alpha-48271'; +$wp_version = '5.5-alpha-48272'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.