wp_trim_words( string $text, int $num_words = 55, string $more = null ): string  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.   Parameters   $text string Required    Text to trim.    $num_words int Optional    Number of words.   Default: 55   $more string Optional     What to append if $text needs to be trimmed. Default '…'.  Default: null     Return string Trimmed text.   Source  File: wp-includes/formatting.php. View all references  function wp_trim_words( $text, $num_words = 55, $more = null ) {
	if ( null === $more ) {
		$more = __( '&hellip;' );
	}

	$original_text = $text;
	$text          = wp_strip_all_tags( $text );
	$num_words     = (int) $num_words;

	/*
	 * translators: If your word count is based on single characters (e.g. East Asian characters),
	 * enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.
	 * Do not translate into your own language.
	 */
	if ( strpos( _x( 'words', 'Word count type. Do not translate!' ), 'characters' ) === 0 && 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. &hellip;.
	 * @param string $original_text The text before it was trimmed.
	 */
	return apply_filters( 'wp_trim_words', $text, $num_words, $more, $original_text );
}
   Hooks     apply_filters( 'wp_trim_words', string $text, int $num_words, string $more, string $original_text )   
Filters the text content after words have been trimmed.      Related  Uses    Uses Description      wp_strip_all_tags() wp-includes/formatting.php   Properly strips all HTML tags including script and style     __() wp-includes/l10n.php   Retrieves the translation of $text.     _x() wp-includes/l10n.php   Retrieves translated string with gettext context.     apply_filters() wp-includes/plugin.php   Calls the callback functions that have been added to a filter hook.     get_option() wp-includes/option.php   Retrieves an option value based on an option name.   
   Used By    Used By Description      WP_REST_Block_Directory_Controller8b7c:f320:99b9:690f:4595:cd17:293a:c069prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-block-directory-controller.php   Parse block metadata for a block, and prepare it for an API response.     WP_Customize_Nav_Menu_Item_Setting8b7c:f320:99b9:690f:4595:cd17:293a:c069value_as_wp_post_nav_menu_item() wp-includes/customize/class-wp-customize-nav-menu-item-setting.php   Get the value emulated into a WP_Post and set up as a nav_menu_item.     wp_dashboard_recent_drafts() wp-admin/includes/dashboard.php   Show recent drafts of the user on the dashboard.     wp_trim_excerpt() wp-includes/formatting.php   Generates an excerpt from the content, if needed.     wp_widget_rss_output() wp-includes/widgets.php   Display the RSS entries in a list.     wp_setup_nav_menu_item() wp-includes/nav-menu.php   Decorates a menu item object with the shared navigation menu item properties.     get_comment_excerpt() wp-includes/comment-template.php   Retrieves the excerpt of the given comment.   
    Changelog    Version Description     3.3.0 Introduced.    

    
{'path': 'public/docs/wordpress/functions/wp_trim_words.html', 'source': 'DevDocs.IO'}