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:4029
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
- Scaling
- Constant
- Instructions
- 18
- Plugin surface
- 4 hooks
- Called by
- 0
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5. The body compiles to 106.
Third-party callbacks on 'the_content', 'excerpt_length', 'excerpt_more' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- querycontent query
get_post()called directly - hookthird-party callbacks
apply_filters()called directly - optionoption read or write
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 18 | ->set_bookmark() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 106 | 18 | 3 | |
| 8.5 | 106 | 18 | 3 | 18 more instructions than PHP 8.4 |
| 8.4 | 88 | 12–88 | 3 | 5 fewer instructions than PHP 8.3 |
| 8.3 | 93 | 14–93 | 3 | |
| 8.2 | 93 | 14–93 | 3 | |
| 8.1 | 93 | 14–93 | 3 | |
| 7.4 | 93 | 14–93 | 3 |
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:
- apply_filters( excerpt_length )filterline 4081 (+52 into the body)
Filters the maximum number of words in a post excerpt.
- apply_filters( excerpt_more )filterline 4090 (+61 into the body)
Filters the string in the "more" link displayed after a trimmed excerpt.
- apply_filters( wp_trim_excerpt )filterline 4103 (+74 into the body)
Filters the trimmed excerpt string.
Uses · 10
- get_post()Retrieves post data given a post ID or post object.
- get_the_content()Retrieves the post content.
- strip_shortcodes()Removes all shortcode tags from the given content.
- excerpt_remove_blocks()Parses blocks out of a content string, and renders those appropriate for the excerpt.
- excerpt_remove_footnotes()Parses footnotes markup out of a content string, and renders those appropriate for the excerpt.
- remove_filter()Removes a callback function from a filter hook.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- add_filter()Adds a callback function to a filter hook.
- _x()Retrieves translated string with gettext context.
- wp_trim_words()Trims text to a certain number of words.
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.
Signature, return type and hooks compared across 5 parsed releases.
$post parameter.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 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.