WordPress.org

Make WordPress Core

Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#40482 closed defect (bug) (wontfix)

apply_filters() for 'get_the_excerpt' doesn't consistently trim the excerpt

Reported by: AD_Taylor Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.7.3
Component: General Keywords: close
Focuses: template Cc:
PR Number:

Description

When filtering a custom string with 'get_the_excerpt' the resulting text isn't trimmed to the excerpt length as expected.

Looking at the core, this is because the wp_trim_excerpt function (wp-includes/formatting.php:3288) only trims the $text when a blank string is passed to it. This seems like unexpected behaviour for a function specifically named to trim strings.

To reproduce, create a post with the following content:

---
Etiam laoreet libero sit amet sem tempor, vel dictum odio bibendum. Aenean odio ligula, placerat sodales dui non, tempor dictum lorem. Vestibulum rutrum, velit a placerat imperdiet, erat massa porta urna, a convallis diam lorem non sapien. Vivamus in risus non quam aliquet blandit nec fermentum dolor. Duis ultricies lectus eu cursus fermentum. Sed eget convallis odio. Ut ut dolor nec nisi varius blandit a eget justo. Integer sed tellus eget leo pretium ultricies. Nullam rhoncus ex sit amet dolor pellentesque feugiat. Nullam a eros orci. Etiam egestas est erat, eu pellentesque sapien dignissim vel. Nulla malesuada commodo justo, at egestas purus egestas fermentum.

Vestibulum vitae metus ullamcorper, vehicula urna eu, ullamcorper leo. Sed sit amet eros eget metus bibendum blandit. Praesent ac lacinia purus. Donec laoreet tempus dui id faucibus. Nulla laoreet cursus laoreet. Nulla et arcu ex. Pellentesque cursus non metus a volutpat. Donec pretium orci et metus molestie, ac feugiat lacus auctor. Ut vehicula eu augue eu gravida. In scelerisque risus in rutrum vestibulum. Phasellus egestas augue quis enim varius, ut sagittis massa auctor.
---

Then place this code into the single.php template file:

<?php

// Output 1
the_excerpt();

$text = "Etiam laoreet libero sit amet sem tempor, vel dictum odio bibendum. Aenean odio ligula, placerat sodales dui non, tempor dictum lorem. Vestibulum rutrum, velit a placerat imperdiet, erat massa porta urna, a convallis diam lorem non sapien. Vivamus in risus non quam aliquet blandit nec fermentum dolor. Duis ultricies lectus eu cursus fermentum. Sed eget convallis odio. Ut ut dolor nec nisi varius blandit a eget justo. Integer sed tellus eget leo pretium ultricies. Nullam rhoncus ex sit amet dolor pellentesque feugiat. Nullam a eros orci. Etiam egestas est erat, eu pellentesque sapien dignissim vel. Nulla malesuada commodo justo, at egestas purus egestas fermentum.

Vestibulum vitae metus ullamcorper, vehicula urna eu, ullamcorper leo. Sed sit amet eros eget metus bibendum blandit. Praesent ac lacinia purus. Donec laoreet tempus dui id faucibus. Nulla laoreet cursus laoreet. Nulla et arcu ex. Pellentesque cursus non metus a volutpat. Donec pretium orci et metus molestie, ac feugiat lacus auctor. Ut vehicula eu augue eu gravida. In scelerisque risus in rutrum vestibulum. Phasellus egestas augue quis enim varius, ut sagittis massa auctor.";

// Output 2
$text = apply_filters( 'get_the_excerpt', $text );
$text = apply_filters( 'the_excerpt', $text );
echo $text;

?>

When viewing the post Output 2, on version 4.7.3, will not be trimmed.

The expected result is that the output from Output 1 & Output 2 is the same.

To produce the expected result, the wp_trim_excerpt function should be changed to the following snippet, so that the lines responsible for trimming are outside the if() statement.

/**
 * Generates an excerpt from the content, if needed.
 *
 * The excerpt word amount will be 55 words and if the amount is greater than
 * that, then the string ' [&hellip;]' will be appended to the excerpt. If the string
 * is less than 55 words, then the content will be returned as is.
 *
 * The 55 word limit can be modified by plugins/themes using the {@see 'excerpt_length'} filter
 * The ' [&hellip;]' string can be modified by plugins/themes using the {@see 'excerpt_more'} filter
 *
 * @since 1.5.0
 *
 * @param string $text Optional. The excerpt. If set to empty, an excerpt is generated.
 * @return string The excerpt.
 */
function wp_trim_excerpt( $text = '' ) {
	$raw_excerpt = $text;
	if ( '' == $text ) {
		$text = get_the_content('');

		$text = strip_shortcodes( $text );

		/** This filter is documented in wp-includes/post-template.php */
		$text = apply_filters( 'the_content', $text );
		$text = str_replace(']]>', ']]&gt;', $text);
	}

	/**
	 * Filters the number of words in an excerpt.
	 *
	 * @since 2.7.0
	 *
	 * @param int $number The number of words. Default 55.
	 */
	$excerpt_length = apply_filters( 'excerpt_length', 55 );
	/**
	 * Filters the string in the "more" link displayed after a trimmed excerpt.
	 *
	 * @since 2.9.0
	 *
	 * @param string $more_string The string shown within the more link.
	 */
	$excerpt_more = apply_filters( 'excerpt_more', ' ' . '[&hellip;]' );
	$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
	
	/**
	 * Filters the trimmed excerpt string.
	 *
	 * @since 2.8.0
	 *
	 * @param string $text        The trimmed text.
	 * @param string $raw_excerpt The text prior to trimming.
	 */
	return apply_filters( 'wp_trim_excerpt', $text, $raw_excerpt );
}

Change History (4)

#1 @dougal
2 years ago

This is expected behavior, and working as designed.

The wp_trim_excerpt filter is used when there is no manually set excerpt, and WordPress is automagically creating an excerpt based on the full post content.

By passing a long string through the get_the_excerpt and get_excerpt filters, you are simulating the case where you have entered the same long text into the Excerpt field in the Edit Post screen. When a custom excerpt is present in the $post, no trimming is done (though other content filters are applied).

If you want to simulate the automatic excerpt trimming on arbitrary text from another source, then you need to pass it through wp_trim_words() yourself.

Last edited 2 years ago by dougal (previous) (diff)

#2 @AD_Taylor
2 years ago

  • Keywords close added

Thanks @dougal. This makes sense now that it has been explained.

I'll make a request to add this information to the codex instead as it may be useful for future users.

#3 @dougal
2 years ago

  • Resolution set to wontfix
  • Status changed from new to closed

#4 @netweb
2 years ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.