Make WordPress Core


Ignore:
Timestamp:
04/08/2020 12:53:18 AM (7 years ago)
Author:
azaozz
Message:

Media: Enable lazy-loading of images by automatically adding the new loading="lazy" attribute to image tags on the front-end.

  • Introduces wp_lazy_loading_enabled(), wp_filter_content_tags(), wp_img_tag_add_loading_attr(), and wp_img_tag_add_srcset_and_sizes_attr() functions.
  • Introduces wp_lazy_loading_enabled, wp_img_tag_add_loading_attr, and wp_img_tag_add_srcset_and_sizes_attr filters.

Props flixos90, addyosmani, mor10, swissspidy, pierlo, westonruter, spacedmonkey, mikeschroder, jonoaldersonwp, peterwilsoncc, narwen, jeffpaul, OptimizingMatters, futtta, mukeshpanchal27, azaozz.

Fixes #44427.

File:
1 edited

Legend:

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

    r47553 r47554  
    26072607         *     @type bool         $force_display Whether to always show the avatar - ignores the show_avatars option.
    26082608         *                                       Default false.
     2609         *     @type string       $loading       Value for the `loading` attribute.
     2610         *                                       Default null.
    26092611         *     @type string       $extra_attr    HTML attributes to insert in the IMG element. Is not sanitized. Default empty.
    26102612         * }
    … …  
    26242626                        'class'         => null,
    26252627                        'force_display' => false,
     2628                        'loading'       => null,
    26262629                        'extra_attr'    => '',
    26272630                );
     2631
     2632                if ( wp_lazy_loading_enabled( 'img', 'get_avatar' ) ) {
     2633                        $defaults['loading'] = 'lazy';
     2634                }
    26282635
    26292636                if ( empty( $args ) ) {
    … …  
    26942701                                $class[] = $args['class'];
    26952702                        }
     2703                }
     2704
     2705                // Add `loading` attribute.
     2706                $extra_attr = $args['extra_attr'];
     2707                $loading    = $args['loading'];
     2708
     2709                if ( in_array( $loading, array( 'lazy', 'eager' ), true ) && ! preg_match( '/\bloading\s*=/', $extra_attr ) ) {
     2710                        if ( ! empty( $extra_attr ) ) {
     2711                                $extra_attr .= ' ';
     2712                        }
     2713
     2714                        $extra_attr .= "loading='{$loading}'";
    26962715                }
    26972716
    … …  
    27042723                        (int) $args['height'],
    27052724                        (int) $args['width'],
    2706                         $args['extra_attr']
     2725                        $extra_attr
    27072726                );
    27082727
Note: See TracChangeset for help on using the changeset viewer.