# _sanitize_text_fields()

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

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

## Сигнатура

```php
_sanitize_text_fields( string $str, bool $keep_newlines = false ): string
```

## Описание

Внутренняя вспомогательная функция для очистки строки из пользовательского ввода или из базы данных.

## Параметры

- `$str` `string` — обязательный. Строка для очистки.
- `$keep_newlines` `bool` — необязательный, по умолчанию `false`. Сохранять ли переносы строк. Значение по умолчанию: false.

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

`string`

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

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

```php
function _sanitize_text_fields( $str, $keep_newlines = false ) {
	if ( is_object( $str ) || is_array( $str ) ) {
		return '';
	}

	$str = (string) $str;

	$filtered = wp_check_invalid_utf8( $str );

	if ( str_contains( $filtered, '<' ) ) {
		$filtered = wp_pre_kses_less_than( $filtered );
		// This will strip extra whitespace for us.
		$filtered = wp_strip_all_tags( $filtered, false );

		/*
		 * Use HTML entities in a special case to make sure that
		 * later newline stripping stages cannot lead to a functional tag.
		 */
		$filtered = str_replace( "<\n", "&lt;\n", $filtered );
	}

	if ( ! $keep_newlines ) {
		$filtered = preg_replace( '/[\r\n\t ]+/', ' ', $filtered );
	}
	$filtered = trim( $filtered );

	// Remove percent-encoded characters.
	$found = false;
	while ( preg_match( '/%[a-f0-9]{2}/i', $filtered, $match ) ) {
		$filtered = str_replace( $match[0], '', $filtered );
		$found    = true;
	}

	if ( $found ) {
		// Strip out the whitespace that may now exist after removing percent-encoded characters.
		$filtered = trim( preg_replace( '/ +/', ' ', $filtered ) );
	}

	return $filtered;
}
```

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

- 4.7.0 — Introduced.

## Связанные

Использует: [`wp_strip_all_tags`](https://chugunov.pro/api-wordpress/functions/wp_strip_all_tags/), [`wp_pre_kses_less_than`](https://chugunov.pro/api-wordpress/functions/wp_pre_kses_less_than/), [`wp_check_invalid_utf8`](https://chugunov.pro/api-wordpress/functions/wp_check_invalid_utf8/).
Используется в: [`sanitize_textarea_field`](https://chugunov.pro/api-wordpress/functions/sanitize_textarea_field/), [`sanitize_text_field`](https://chugunov.pro/api-wordpress/functions/sanitize_text_field/).

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