WordPress.org

Make WordPress Core


Ignore:
Timestamp:
07/02/2020 02:01:28 AM (4 months ago)
Author:
flixos90
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/media.php

    r48239 r48272  
    10491049        $attr = wp_parse_args( $attr, $default_attr );
    10501050
     1051        // If `loading` attribute default of `lazy` is overridden for this
     1052        // image to omit the attribute, ensure it is not included.
     1053        if ( array_key_exists( 'loading', $attr ) && ! $attr['loading'] ) {
     1054            unset( $attr['loading'] );
     1055        }
     1056
    10511057        // Generate 'srcset' and 'sizes' if not already present.
    10521058        if ( empty( $attr['srcset'] ) ) {
     
    17261732     * @since 5.5.0
    17271733     *
    1728      * @param string $value   The `loading` attribute value, defaults to `lazy`.
    1729      * @param string $image   The HTML `img` tag to be filtered.
    1730      * @param string $context Additional context about how the function was called or where the img tag is.
     1734     * @param string|bool $value   The `loading` attribute value. Returning a false-y value will result in the
     1735     *                             attribute being omitted for the image. Default is `lazy`.
     1736     * @param string      $image   The HTML `img` tag to be filtered.
     1737     * @param string      $context Additional context about how the function was called or where the img tag is.
    17311738     */
    17321739    $value = apply_filters( 'wp_img_tag_add_loading_attr', 'lazy', $image, $context );
Note: See TracChangeset for help on using the changeset viewer.