Make WordPress Core


Ignore:
Timestamp:
09/25/2023 10:37:00 PM (5 months ago)
Author:
flixos90
Message:

Media: Rely on wp_get_loading_optimization_attributes() to add decoding="async" to images.

The wp_get_loading_optimization_attributes() function was introduced in 6.3, as a single centralized place to control loading optimization attributes for various tags, most importantly images.

This changeset consolidates the decoding="async" optimization, which was added in 6.1, to occur solely as part of wp_get_loading_optimization_attributes(), removing duplicate code and allowing centralized filtering based on [56651].

As part of the change, the wp_img_tag_add_decoding_attr() function has been deprecated. The filter of the same name continues to be maintained for backward compatibility, as before covering only images that are part of a content blob such as post content (the_content).

Props pereirinha, mukesh27, joemcgill, flixos90.
Fixes #58892.
See #53232.

File:
1 edited

Legend:

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

    r56682 r56690  
    59955995    update_option( 'https_detection_errors', $support_errors );
    59965996}
     5997
     5998/**
     5999 * Adds `decoding` attribute to an `img` HTML tag.
     6000 *
     6001 * The `decoding` attribute allows developers to indicate whether the
     6002 * browser can decode the image off the main thread (`async`), on the
     6003 * main thread (`sync`) or as determined by the browser (`auto`).
     6004 *
     6005 * By default WordPress adds `decoding="async"` to images but developers
     6006 * can use the {@see 'wp_img_tag_add_decoding_attr'} filter to modify this
     6007 * to remove the attribute or set it to another accepted value.
     6008 *
     6009 * @since 6.1.0
     6010 * @deprecated 6.4.0 Use wp_img_tag_add_loading_optimization_attrs() instead.
     6011 * @see wp_img_tag_add_loading_optimization_attrs()
     6012 *
     6013 * @param string $image   The HTML `img` tag where the attribute should be added.
     6014 * @param string $context Additional context to pass to the filters.
     6015 * @return string Converted `img` tag with `decoding` attribute added.
     6016 */
     6017function wp_img_tag_add_decoding_attr( $image, $context ) {
     6018    _deprecated_function( __FUNCTION__, '6.4.0', 'wp_img_tag_add_loading_optimization_attrs()' );
     6019
     6020    /*
     6021     * Only apply the decoding attribute to images that have a src attribute that
     6022     * starts with a double quote, ensuring escaped JSON is also excluded.
     6023     */
     6024    if ( ! str_contains( $image, ' src="' ) ) {
     6025        return $image;
     6026    }
     6027
     6028    /** This action is documented in wp-includes/media.php */
     6029    $value = apply_filters( 'wp_img_tag_add_decoding_attr', 'async', $image, $context );
     6030
     6031    if ( in_array( $value, array( 'async', 'sync', 'auto' ), true ) ) {
     6032        $image = str_replace( '<img ', '<img decoding="' . esc_attr( $value ) . '" ', $image );
     6033    }
     6034
     6035    return $image;
     6036}
Note: See TracChangeset for help on using the changeset viewer.