Make WordPress Core


Ignore:
Timestamp:
04/07/2021 12:59:18 AM (4 years ago)
Author:
peterwilsoncc
Message:

Media: Do not lazy load hidden images or embeds.

Improve the check for sourceless or dimensionless media when determining if the lazy loading attribute should be added to iframes and images. Never include the lazy loading attribute on embeds of WordPress posts as the iframe is initially hidden.

Including loading="lazy" on initially hidden iframes and images can prevent the media from loading in some browsers.

Props adamsilverstein, fabianpimminger, flixos90, johnbillion, jonkastonka, joyously, peterwilsoncc, SergeyBiryukov, SirStuey, swissspidy.
Fixes #52768.

File:
1 edited

Legend:

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

    r50586 r50682  
    18701870 */
    18711871function wp_img_tag_add_loading_attr( $image, $context ) {
     1872    // Images should have source and dimension attributes for the `loading` attribute to be added.
     1873    if ( false === strpos( $image, ' src="' ) || false === strpos( $image, ' width="' ) || false === strpos( $image, ' height="' ) ) {
     1874        return $image;
     1875    }
     1876
    18721877    /**
    18731878     * Filters the `loading` attribute value to add to an image. Default `lazy`.
     
    18901895        }
    18911896
    1892         // Images should have source and dimension attributes for the `loading` attribute to be added.
    1893         if ( false === strpos( $image, ' src="' ) || false === strpos( $image, ' width="' ) || false === strpos( $image, ' height="' ) ) {
    1894             return $image;
    1895         }
    1896 
    18971897        return str_replace( '<img', '<img loading="' . esc_attr( $value ) . '"', $image );
    18981898    }
     
    19901990 */
    19911991function wp_iframe_tag_add_loading_attr( $iframe, $context ) {
     1992    // Iframes with fallback content (see `wp_filter_oembed_result()`) should not be lazy-loaded because they are
     1993    // visually hidden initially.
     1994    if ( false !== strpos( $iframe, ' data-secret="' ) ) {
     1995        return $iframe;
     1996    }
     1997
     1998    // Iframes should have source and dimension attributes for the `loading` attribute to be added.
     1999    if ( false === strpos( $iframe, ' src="' ) || false === strpos( $iframe, ' width="' ) || false === strpos( $iframe, ' height="' ) ) {
     2000        return $iframe;
     2001    }
     2002
    19922003    /**
    19932004     * Filters the `loading` attribute value to add to an iframe. Default `lazy`.
     
    20082019        if ( ! in_array( $value, array( 'lazy', 'eager' ), true ) ) {
    20092020            $value = 'lazy';
    2010         }
    2011 
    2012         // Iframes should have source and dimension attributes for the `loading` attribute to be added.
    2013         if ( false === strpos( $iframe, ' src="' ) || false === strpos( $iframe, ' width="' ) || false === strpos( $iframe, ' height="' ) ) {
    2014             return $iframe;
    20152021        }
    20162022
Note: See TracChangeset for help on using the changeset viewer.