Make WordPress Core


Ignore:
Timestamp:
09/26/2023 12:11:06 AM (19 months ago)
Author:
flixos90
Message:

Media: Ensure images within shortcodes are correctly considered for loading optimization attributes.

Prior to this change, images added in shortcodes would be considered separately from all other images within post content, which led to incorrect application of the loading optimization attributes loading="lazy" and fetchpriority="high".

This changeset changes the filter priority of wp_filter_content_tags() from the default 10 to 12 on the various content filters it is hooked in, in order to run that function after parsing shortcodes. While this may technically be considered a backward compatibility break, substantial research and lack of any relevant usage led to the assessment that the change is acceptable given its benefits.

An additional related fix included is that now the duplicate processing of images is prevented not only for post content blobs (the_content filter), but also for widget content blobs (widget_text_content and widget_block_content filters).

Props joemcgill, mukesh27, costdev, spacedmonkey, flixos90.
Fixes #58853.

File:
1 edited

Legend:

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

    r56690 r56693  
    56505650
    56515651    /*
    5652      * Skip programmatically created images within post content as they need to be handled together with the other
    5653      * images within the post content.
     5652     * Skip programmatically created images within content blobs as they need to be handled together with the other
     5653     * images within the post content or widget content.
    56545654     * Without this clause, they would already be considered within their own context which skews the image count and
    56555655     * can result in the first post content image being lazy-loaded or an image further down the page being marked as a
    56565656     * high priority.
    56575657     */
    5658     // TODO: Handle shortcode images together with the content (see https://core.trac.wordpress.org/ticket/58853).
    5659     if ( 'the_content' !== $context && 'do_shortcode' !== $context && doing_filter( 'the_content' ) ) {
     5658    if (
     5659        'the_content' !== $context && doing_filter( 'the_content' ) ||
     5660        'widget_text_content' !== $context && doing_filter( 'widget_text_content' ) ||
     5661        'widget_block_content' !== $context && doing_filter( 'widget_block_content' )
     5662    ) {
    56605663        /** This filter is documented in wp-includes/media.php */
    56615664        return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context );
     5665
    56625666    }
    56635667
Note: See TracChangeset for help on using the changeset viewer.