Make WordPress Core


Ignore:
Timestamp:
05/23/2023 06:23:59 PM (22 months ago)
Author:
flixos90
Message:

Media: Fix lazy-loading bug by avoiding to modify content images when creating an excerpt.

The wp_filter_content_tags() function, which modifies image tags for example to optimize performance, is hooked into the the_content filter by default. When rendering an excerpt for a post that doesn't have a manually provided excerpt, the post content is used to generate the excerpt, handled by the wp_trim_excerpt() function.

Prior to this changeset, this led to wp_filter_content_tags() being called on the content when generating the excerpt, which is wasteful as all tags are stripped from the excerpt, and it furthermore could result in a lazy-loading bug when the post content contained images, as those images were being counted even though they would never be rendered as part of the excerpt.

This changeset fixes the bug and slightly improves performance for generating an excerpt by temporarily unhooking the wp_filter_content_tags() function from the the_content filter when using it to generate the excerpt.

Props costdev, flixos90, joemcgill, mukesh27, salvoaranzulla, spacedmonkey, thekt12, westonruter.
Fixes #56588.

File:
1 edited

Legend:

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

    r55844 r55850  
    39383938        $text = excerpt_remove_blocks( $text );
    39393939
     3940        /*
     3941         * Temporarily unhook wp_filter_content_tags() since any tags
     3942         * within the excerpt are stripped out. Modifying the tags here
     3943         * is wasteful and can lead to bugs in the image counting logic.
     3944         */
     3945        $filter_removed = remove_filter( 'the_content', 'wp_filter_content_tags' );
     3946
    39403947        /** This filter is documented in wp-includes/post-template.php */
    39413948        $text = apply_filters( 'the_content', $text );
    39423949        $text = str_replace( ']]>', ']]>', $text );
     3950
     3951        /**
     3952         * Only restore the filter callback if it was removed above. The logic
     3953         * to unhook and restore only applies on the default priority of 10,
     3954         * which is generally used for the filter callback in WordPress core.
     3955         */
     3956        if ( $filter_removed ) {
     3957            add_filter( 'the_content', 'wp_filter_content_tags' );
     3958        }
    39433959
    39443960        /* translators: Maximum number of words used in a post excerpt. */
Note: See TracChangeset for help on using the changeset viewer.