# esc_js()

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

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

## Сигнатура

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

## Описание

Экранирует текстовые строки для вывода в JS. Предназначена для встроенного JS (в атрибуте тега, например onclick="..."). Учтите, что строки должны быть заключены в одинарные кавычки. Здесь также применяется фильтр ‘js_escape’.

## Параметры

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

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

`string`

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

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

```php
function esc_js( $text ) {
	$safe_text = wp_check_invalid_utf8( $text );
	$safe_text = _wp_specialchars( $safe_text, ENT_COMPAT );
	$safe_text = preg_replace( '/&#(x)?0*(?(1)27|39);?/i', "'", stripslashes( $safe_text ) );
	$safe_text = str_replace( "\r", '', $safe_text );
	$safe_text = str_replace( "\n", '\\n', addslashes( $safe_text ) );
	/**
	 * Filters a string cleaned and escaped for output in JavaScript.
	 *
	 * Text passed to esc_js() is stripped of invalid or special characters,
	 * and properly slashed for output.
	 *
	 * @since 2.0.6
	 *
	 * @param string $safe_text The text after it has been escaped.
	 * @param string $text      The text prior to being escaped.
	 */
	return apply_filters( 'js_escape', $safe_text, $text );
}
```

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

- 2.8.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_default_packages_vendor`](https://chugunov.pro/api-wordpress/functions/wp_default_packages_vendor/), `WP_Customize_Site_Icon_Control::content_template`, `WP_Links_List_Table::handle_row_actions`, `Bulk_Upgrader_Skin::before`, `Bulk_Upgrader_Skin::after`, `Bulk_Upgrader_Skin::error`, [`_thickbox_path_admin_subfolder`](https://chugunov.pro/api-wordpress/functions/_thickbox_path_admin_subfolder/), [`wp_save_image`](https://chugunov.pro/api-wordpress/functions/wp_save_image/), [`iframe_header`](https://chugunov.pro/api-wordpress/functions/iframe_header/), `WP_Themes_List_Table::display_rows`, [`wp_iframe`](https://chugunov.pro/api-wordpress/functions/wp_iframe/), [`link_submit_meta_box`](https://chugunov.pro/api-wordpress/functions/link_submit_meta_box/), `Custom_Image_Header::js_1`, [`dismissed_updates`](https://chugunov.pro/api-wordpress/functions/dismissed_updates/), [`js_escape`](https://chugunov.pro/api-wordpress/functions/js_escape/), [`sanitize_term_field`](https://chugunov.pro/api-wordpress/functions/sanitize_term_field/), `WP_Admin_Bar::_render_item`, [`sanitize_user_field`](https://chugunov.pro/api-wordpress/functions/sanitize_user_field/), [`sanitize_post_field`](https://chugunov.pro/api-wordpress/functions/sanitize_post_field/), [`sanitize_bookmark_field`](https://chugunov.pro/api-wordpress/functions/sanitize_bookmark_field/), [`wp_print_media_templates`](https://chugunov.pro/api-wordpress/functions/wp_print_media_templates/).

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