wp_trim_words( string $text, int $num_words = 55, string $more = null ): string
- Since
- 3.3.0
- Source
wp-includes/formatting.php:4049
Description
This function is localized. For languages that count 'words' by the individual character (such as East Asian languages), the $num_words argument will apply to the number of individual characters.
Compatibility
- WordPress
- since 3.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
$textstring- Text to trim.
$num_wordsintoptional- Number of words. Default 55.Default:
55 $morestringoptional- What to append if $text needs to be trimmed. Default '…'.Default:
null
Return value
string- Trimmed text.
Performance profile
How much work a call to wp_trim_words() 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
- Moderate
- Scaling
- Constant
- Instructions
- 38–63
- Plugin surface
- 1 hook
- Called by
- 10
Reads stored settings via get_option(), cached per request but not free on a cold cache.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 74.
Third-party callbacks on 'wp_trim_words' run inside this call, and their cost is not bounded by anything here.
10 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- optionoption read or write
get_option()called directly - hookthird-party callbacks
apply_filters()called directly - regexregular expression over the whole input
preg_split()called directly - cacheobject cache
wp_cache_get()one call below wp_trim_words() - serializeserialisation
maybe_unserialize()one call below wp_trim_words()
Further down the call graph this can also reach query 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 · 12 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_trim_words() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$more !== null && !$num_words | 38 | wp_strip_all_tags(), wp_get_word_count_type(), preg_split(), apply_filters() |
$more === null && !$num_words | 42 | __(), wp_strip_all_tags(), wp_get_word_count_type(), preg_split(), apply_filters() |
$more !== null && !$num_words | 43 | wp_strip_all_tags(), wp_get_word_count_type(), get_option(), preg_split(), apply_filters() |
$more !== null && $num_words | 44 | wp_strip_all_tags(), wp_get_word_count_type(), preg_split(), array_pop(), apply_filters() |
$more === null && !$num_words | 47 | __(), wp_strip_all_tags(), wp_get_word_count_type(), get_option(), preg_split(), apply_filters() |
$more === null && $num_words | 48 | __(), wp_strip_all_tags(), wp_get_word_count_type(), preg_split(), array_pop(), apply_filters() |
$more !== null && $num_words | 49 | wp_strip_all_tags(), wp_get_word_count_type(), get_option(), preg_split(), array_pop(), apply_filters() |
$more !== null && !$num_words | 53 | wp_strip_all_tags(), wp_get_word_count_type(), get_option(), preg_match_all(), array_slice(), apply_filters() |
$more === null && $num_words | 53 | __(), wp_strip_all_tags(), wp_get_word_count_type(), get_option(), preg_split(), array_pop(), apply_filters() |
$more === null && !$num_words | 57 | __(), wp_strip_all_tags(), wp_get_word_count_type(), get_option(), preg_match_all(), array_slice(), apply_filters() |
$more !== null && $num_words | 59 | wp_strip_all_tags(), wp_get_word_count_type(), get_option(), preg_match_all(), array_slice(), array_pop(), apply_filters() |
$more === null && $num_words | 63 | __(), wp_strip_all_tags(), wp_get_word_count_type(), get_option(), preg_match_all(), array_slice(), array_pop(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 74 | 38–63 | 4 | |
| 8.5 | 74 | 38–63 | 4 | |
| 8.4 | 74 | 38–63 | 4 | 18 fewer instructions than PHP 8.3 |
| 8.3 | 92 | 44–78 | 4 | |
| 8.2 | 92 | 44–78 | 4 | |
| 8.1 | 92 | 44–78 | 4 | 1 more instruction than PHP 7.4 |
| 7.4 | 91 | 44–77 | 4 |
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 · 1
One hook fires while wp_trim_words() runs, in this order:
- apply_filters( wp_trim_words )filterline 4086 (+37 into the body)
Filters the text content after words have been trimmed.
Uses · 6
- __()Retrieves the translation of $text.
- wp_strip_all_tags()Properly strips all HTML tags including 'script' and 'style'.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- wp_get_word_count_type()Retrieves the word count type based on the locale.
- get_option()Retrieves an option value based on an option name.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 10
- WP_Customize_Nav_Menu_Item_Setting::value_as_wp_post_nav_menu_item()Get the value emulated into a WP_Post and set up as a nav_menu_item.
- WP_REST_Block_Directory_Controller::prepare_item_for_response()Parse block metadata for a block, and prepare it for an API response.
- get_comment_excerpt()Retrieves the excerpt of the given comment.
- render_block_core_breadcrumbs()Renders the `core/breadcrumbs` block on the server.
- render_block_core_post_excerpt()Renders the `core/post-excerpt` block on the server.
- render_block_core_rss()Renders the `core/rss` block on server.
- wp_dashboard_recent_drafts()Show recent drafts of the user on the dashboard.
- wp_setup_nav_menu_item()Decorates a menu item object with the shared navigation menu item properties.
- wp_trim_excerpt()Generates an excerpt from the content, if needed.
- wp_widget_rss_output()Displays the RSS entries in a list.
Source code
function wp_trim_words( $text, $num_words = 55, $more = null ) { if ( null === $more ) { $more = __( '…' ); } $original_text = $text; $text = wp_strip_all_tags( $text ); $num_words = (int) $num_words; if ( str_starts_with( wp_get_word_count_type(), 'characters' ) && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) { $text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' ); preg_match_all( '/./u', $text, $words_array ); $words_array = array_slice( $words_array[0], 0, $num_words + 1 ); $sep = ''; } else { $words_array = preg_split( "/[\n\r\t ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY ); $sep = ' '; } if ( count( $words_array ) > $num_words ) { array_pop( $words_array ); $text = implode( $sep, $words_array ); $text = $text . $more; } else { $text = implode( $sep, $words_array ); } /** * Filters the text content after words have been trimmed. * * @since 3.3.0 * * @param string $text The trimmed text. * @param int $num_words The number of words to trim the text to. Default 55. * @param string $more An optional string to append to the end of the trimmed text, e.g. …. * @param string $original_text The text before it was trimmed. */ return apply_filters( 'wp_trim_words', $text, $num_words, $more, $original_text );}Changelog
Introduced in 3.3.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 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.