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
This commit is contained in:
Felix Arntz 2020-07-02 02:03:03 +00:00
parent 55b80358a5
commit 1d010a49c7
2 changed files with 11 additions and 4 deletions

View File

@ -1048,6 +1048,12 @@ function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = f
$attr = wp_parse_args( $attr, $default_attr ); $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. // Generate 'srcset' and 'sizes' if not already present.
if ( empty( $attr['srcset'] ) ) { if ( empty( $attr['srcset'] ) ) {
$image_meta = wp_get_attachment_metadata( $attachment_id ); $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 * @since 5.5.0
* *
* @param string $value The `loading` attribute value, defaults to `lazy`. * @param string|bool $value The `loading` attribute value. Returning a false-y value will result in the
* @param string $image The HTML `img` tag to be filtered. * attribute being omitted for the image. Default is `lazy`.
* @param string $context Additional context about how the function was called or where the img tag is. * @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 ); $value = apply_filters( 'wp_img_tag_add_loading_attr', 'lazy', $image, $context );

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @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. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.