WordPress.org

Make WordPress Core


Ignore:
Timestamp:
03/20/2019 03:48:46 PM (10 months ago)
Author:
boonebgorges
Message:

Posts: Avoid the use of globals in get_the_content() and related functions.

This changeset introduces $post parameters to get_the_content() and
wp_trim_excerpt(). When a $post object is passed to one of these functions,
the functions will operate on the data from that object, rather than from the
post globals ($authordata, $page, etc). This ensures that the functions work
in a predictable manner when used outside of the regular post loop.

The global-mismatch problem is surfaced in cases where get_the_excerpt() is
called outside of the post loop, on posts that don't have a defined excerpt. In
these cases, the post globals - used to generate a fallback excerpt - may refer
to the incorrect object, resulting in PHP notices or other unpredictable
behavior. See #36934 for a related issue.

Props spacedmonkey, kraftbj, Shital Patel.
Fixes #42814.

File:
1 edited

Legend:

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

    r44714 r44941  
    183183add_filter( 'the_excerpt', 'wpautop' );
    184184add_filter( 'the_excerpt', 'shortcode_unautop' );
    185 add_filter( 'get_the_excerpt', 'wp_trim_excerpt' );
     185add_filter( 'get_the_excerpt', 'wp_trim_excerpt', 10, 2 );
    186186
    187187add_filter( 'the_post_thumbnail_caption', 'wptexturize' );
Note: See TracChangeset for help on using the changeset viewer.