# wp_trim_words()

URL: https://chugunov.pro/api-wordpress/functions/wp_trim_words/
Проверено на WordPress 6.9, обновлено 06.08.2026.
Источник: независимый русскоязычный справочник chugunov.pro. Не является официальной документацией WordPress.

Тип: функция.
Появился в версии: 3.3.0.

## Сигнатура

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

## Описание

Эта функция локализована. Для языков, где «слова» считаются по отдельным символам (например, восточноазиатские языки), аргумент $num_words будет применяться к количеству отдельных символов.

## Параметры

- `$text` `string` — обязательный. Текст для обрезки.
- `$num_words` `int` — необязательный, по умолчанию `55`. Количество слов.
- `$more` `string` — необязательный, по умолчанию `null`. Что добавить, если $text требуется обрезать. Значение по умолчанию — ‘…’.

## Возвращаемое значение

`string`

## Исходный код

Файл: `wp-includes/formatting.php:4049`

```php
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;

	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. &hellip;.
	 * @param string $original_text The text before it was trimmed.
	 */
	return apply_filters( 'wp_trim_words', $text, $num_words, $more, $original_text );
}
```

## История изменений

- 3.3.0 — Introduced.

## Связанные

Использует: [`wp_get_word_count_type`](https://chugunov.pro/api-wordpress/functions/wp_get_word_count_type/), [`wp_strip_all_tags`](https://chugunov.pro/api-wordpress/functions/wp_strip_all_tags/), [`__`](https://chugunov.pro/api-wordpress/functions/__/), [`apply_filters`](https://chugunov.pro/api-wordpress/functions/apply_filters/), [`get_option`](https://chugunov.pro/api-wordpress/functions/get_option/).
Используется в: `WP_REST_Block_Directory_Controller::prepare_item_for_response`, `WP_Customize_Nav_Menu_Item_Setting::value_as_wp_post_nav_menu_item`, `WP_Post`, [`wp_dashboard_recent_drafts`](https://chugunov.pro/api-wordpress/functions/wp_dashboard_recent_drafts/), [`wp_trim_excerpt`](https://chugunov.pro/api-wordpress/functions/wp_trim_excerpt/), [`wp_widget_rss_output`](https://chugunov.pro/api-wordpress/functions/wp_widget_rss_output/), [`wp_setup_nav_menu_item`](https://chugunov.pro/api-wordpress/functions/wp_setup_nav_menu_item/), [`get_comment_excerpt`](https://chugunov.pro/api-wordpress/functions/get_comment_excerpt/).

Оригинал в официальной документации: https://developer.wordpress.org/reference/functions/wp_trim_words/
