wppaste
WordPress

wp_trim_excerpt( string $text = '', WP_Post|object|int $post = null ): string

Since
1.5.0, 5.2.0, 6.3.0
Source
wp-includes/formatting.php:3940
Generates an excerpt from the content, if needed.

Description

Returns a maximum of 55 words with an ellipsis appended if necessary.

The 55-word limit can be modified by plugins/themes using the 'excerpt_length' filter The ' […]' string can be modified by plugins/themes using the 'excerpt_more' filter

Compatibility

WordPress
since 6.3.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$textstringoptional
The excerpt. If set to empty, an excerpt is generated.Default: ''
$postWP_Post|object|intoptional
WP_Post instance or Post ID/object. Default null.Default: null

Return value

string
The excerpt.

Performance profile

How much work a call to wp_trim_excerpt() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_post().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
18

Executed per call on PHP 8.5. The body compiles to 106.

Plugin surface
4 hooks

Third-party callbacks on 'the_content', 'excerpt_length', 'excerpt_more' run inside this call, and their cost is not bounded by anything here.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • querycontent queryget_post()called directly
  • hookthird-party callbacksapply_filters()called directly
  • optionoption read or writeget_option()one call below wp_trim_excerpt()

Further down the call graph this can also reach cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_trim_excerpt() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always18->set_bookmark()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev106183
8.510618318 more instructions than PHP 8.4
8.48812–8835 fewer instructions than PHP 8.3
8.39314–933
8.29314–933
8.19314–933
7.49314–933

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 4

4 hooks fire while wp_trim_excerpt() runs, in this order:

  1. apply_filters( the_content )filterline 3965 (+25 into the body)

    Filters the post content.

  2. apply_filters( excerpt_length )filterline 3992 (+52 into the body)

    Filters the maximum number of words in a post excerpt.

  3. apply_filters( excerpt_more )filterline 4001 (+61 into the body)

    Filters the string in the "more" link displayed after a trimmed excerpt.

  4. apply_filters( wp_trim_excerpt )filterline 4014 (+74 into the body)

    Filters the trimmed excerpt string.

Uses · 10

Source code

function wp_trim_excerpt( $text = '', $post = null ) {	$raw_excerpt = $text; 	if ( '' === trim( $text ) ) {		$post = get_post( $post );		$text = get_the_content( '', false, $post ); 		$text = strip_shortcodes( $text );		$text = excerpt_remove_blocks( $text );		$text = excerpt_remove_footnotes( $text ); 		/*		 * Temporarily unhook wp_filter_content_tags() since any tags		 * within the excerpt are stripped out. Modifying the tags here		 * is wasteful and can lead to bugs in the image counting logic.		 */		$filter_image_removed = remove_filter( 'the_content', 'wp_filter_content_tags', 12 ); 		/*		 * Temporarily unhook do_blocks() since excerpt_remove_blocks( $text )		 * handles block rendering needed for excerpt.		 */		$filter_block_removed = remove_filter( 'the_content', 'do_blocks', 9 ); 		/** This filter is documented in wp-includes/post-template.php */		$text = apply_filters( 'the_content', $text );		$text = str_replace( ']]>', ']]>', $text ); 		// Restore the original filter if removed.		if ( $filter_block_removed ) {			add_filter( 'the_content', 'do_blocks', 9 );		} 		/*		 * Only restore the filter callback if it was removed above. The logic		 * to unhook and restore only applies on the default priority of 10,		 * which is generally used for the filter callback in WordPress core.		 */		if ( $filter_image_removed ) {			add_filter( 'the_content', 'wp_filter_content_tags', 12 );		} 		/* translators: Maximum number of words used in a post excerpt. */		$excerpt_length = (int) _x( '55', 'excerpt_length' ); 		/**		 * Filters the maximum number of words in a post excerpt.		 *		 * @since 2.7.0		 *		 * @param int $number The maximum number of words. Default 55.		 */		$excerpt_length = (int) apply_filters( 'excerpt_length', $excerpt_length ); 		/**		 * 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', ' ' . '[…]' );		$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 );}

Changelog

Introduced in 1.5.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.3.0
Removes footnotes markup from the excerpt content.from the docblock
5.2.0
Added the $post parameter.from the docblock
1.5.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/formatting.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.