# esc_xml()

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

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

## Сигнатура

```php
esc_xml( string $text ): string
```

## Описание

Экранирование для XML-блоков.

## Параметры

- `$text` `string` — обязательный. Текст для экранирования.

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

`string`

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

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

```php
function esc_xml( $text ) {
	$safe_text = wp_check_invalid_utf8( $text );

	$cdata_regex = '\<\!\[CDATA\[.*?\]\]\>';
	$regex       = <<<EOF
/
	(?=.*?{$cdata_regex})                 # lookahead that will match anything followed by a CDATA Section
	(?<non_cdata_followed_by_cdata>(.*?)) # the "anything" matched by the lookahead
	(?<cdata>({$cdata_regex}))            # the CDATA Section matched by the lookahead

|	                                      # alternative

	(?<non_cdata>(.*))                    # non-CDATA Section
/sx
EOF;

	$safe_text = (string) preg_replace_callback(
		$regex,
		static function ( $matches ) {
			if ( ! isset( $matches[0] ) ) {
				return '';
			}

			if ( isset( $matches['non_cdata'] ) ) {
				// escape HTML entities in the non-CDATA Section.
				return _wp_specialchars( $matches['non_cdata'], ENT_XML1 );
			}

			// Return the CDATA Section unchanged, escape HTML entities in the rest.
			return _wp_specialchars( $matches['non_cdata_followed_by_cdata'], ENT_XML1 ) . $matches['cdata'];
		},
		$safe_text
	);

	/**
	 * Filters a string cleaned and escaped for output in XML.
	 *
	 * Text passed to esc_xml() is stripped of invalid or special characters
	 * before output. HTML named character references are converted to their
	 * equivalent code points.
	 *
	 * @since 5.5.0
	 *
	 * @param string $safe_text The text after it has been escaped.
	 * @param string $text      The text prior to being escaped.
	 */
	return apply_filters( 'esc_xml', $safe_text, $text );
}
```

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

- 5.5.0 — Introduced.

## Связанные

Использует: [`wp_check_invalid_utf8`](https://chugunov.pro/api-wordpress/functions/wp_check_invalid_utf8/), [`_wp_specialchars`](https://chugunov.pro/api-wordpress/functions/_wp_specialchars/), [`apply_filters`](https://chugunov.pro/api-wordpress/functions/apply_filters/).
Используется в: `WP_Sitemaps_Renderer::get_sitemap_index_xml`, `WP_Sitemaps_Renderer::get_sitemap_xml`, `WP_Sitemaps_Renderer::check_for_simple_xml_availability`, `WP_Sitemaps_Stylesheet::get_sitemap_stylesheet`, `WP_Sitemaps_Stylesheet::get_sitemap_index_stylesheet`.

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