Make WordPress Core


Ignore:
Timestamp:
05/18/2023 07:53:37 PM (7 months ago)
Author:
flixos90
Message:

Media: Prevent special images within post content to skew image counts and cause lazy-loading bugs.

In order to skip lazy-loading the first few images on a page, as of WordPress 5.9 there has been logic to count images that are eligible based on certain criteria. One of those groups are images that appear within the content of a post.

This changeset fixes a bug where images created via get_the_post_thumbnail() or wp_get_attachment_image() that are injected into the post content would skew the count and therefore result in all images to be lazy-loaded, potentially hurting load time performance. This is relevant for example when those functions are called in server-side rendered blocks, or any other filter callbacks hooked into the_content.

Props flixos90, antpb, joedolson, spacedmonkey, mukesh27, thekt12, costdev, jrf.
Fixes #58089.
See #53675.

File:
1 edited

Legend:

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

    r55821 r55825  
    55085508
    55095509    /*
     5510     * Skip programmatically created images within post content as they need to be handled together with the other
     5511     * images within the post content.
     5512     * Without this clause, they would already be counted below which skews the number and can result in the first
     5513     * post content image being lazy-loaded only because there are images elsewhere in the post content.
     5514     */
     5515    if ( ( 'the_post_thumbnail' === $context || 'wp_get_attachment_image' === $context ) && doing_filter( 'the_content' ) ) {
     5516        return false;
     5517    }
     5518
     5519    /*
    55105520     * The first elements in 'the_content' or 'the_post_thumbnail' should not be lazy-loaded,
    55115521     * as they are likely above the fold.
Note: See TracChangeset for help on using the changeset viewer.