wppaste
WordPress

wp_trim_words( string $text, int $num_words = 55, string $more = null ): string

Since
3.3.0
Source
wp-includes/formatting.php:4050
Trims text to a certain number of words.

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

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Constant

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

Instructions
38–63

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 74.

Plugin surface
1 hook

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

Called by
9

9 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • optionoption read or writeget_option()called directly
  • hookthird-party callbacksapply_filters()called directly
  • regexregular expression over the whole inputpreg_split()called directly
  • cacheobject cachewp_cache_get()one call below wp_trim_words()
  • serializeserialisationmaybe_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.

WhenInstructionsCalls it makes
$more !== null && !$num_words38wp_strip_all_tags(), wp_get_word_count_type(), preg_split(), apply_filters()
$more === null && !$num_words42__(), wp_strip_all_tags(), wp_get_word_count_type(), preg_split(), apply_filters()
$more !== null && !$num_words43wp_strip_all_tags(), wp_get_word_count_type(), get_option(), preg_split(), apply_filters()
$more !== null && $num_words44wp_strip_all_tags(), wp_get_word_count_type(), preg_split(), array_pop(), apply_filters()
$more === null && !$num_words47__(), wp_strip_all_tags(), wp_get_word_count_type(), get_option(), preg_split(), apply_filters()
$more === null && $num_words48__(), wp_strip_all_tags(), wp_get_word_count_type(), preg_split(), array_pop(), apply_filters()
$more !== null && $num_words49wp_strip_all_tags(), wp_get_word_count_type(), get_option(), preg_split(), array_pop(), apply_filters()
$more !== null && !$num_words53wp_strip_all_tags(), wp_get_word_count_type(), get_option(), preg_match_all(), array_slice(), apply_filters()
$more === null && $num_words53__(), wp_strip_all_tags(), wp_get_word_count_type(), get_option(), preg_split(), array_pop(), apply_filters()
$more === null && !$num_words57__(), wp_strip_all_tags(), wp_get_word_count_type(), get_option(), preg_match_all(), array_slice(), apply_filters()
$more !== null && $num_words59wp_strip_all_tags(), wp_get_word_count_type(), get_option(), preg_match_all(), array_slice(), array_pop(), apply_filters()
$more === null && $num_words63__(), wp_strip_all_tags(), wp_get_word_count_type(), get_option(), preg_match_all(), array_slice(), array_pop(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev7438–634
8.57438–634
8.47438–63418 fewer instructions than PHP 8.3
8.39244–784
8.29244–784
8.19244–7841 more instruction than PHP 7.4
7.49144–774

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:

  1. apply_filters( wp_trim_words )filterline 4087 (+37 into the body)

    Filters the text content after words have been trimmed.

Uses · 6

Used by · 9

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.

  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.

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 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.