钩子文档

js_escape

💡 云策文档标注

概述

esc_js() 函数用于过滤字符串,使其在 JavaScript 输出中安全转义。该函数会移除无效或特殊字符,并正确添加斜杠。

关键要点

  • esc_js() 转义单引号、双引号、反斜杠、& 符号,并修正换行符
  • js_escape 过滤器允许在转义后修改字符串,参数包括 $safe_text(转义后文本)和 $text(原始文本)
  • 该过滤器自 WordPress 2.0.6 版本引入,位于 wp-includes/formatting.php 文件中

代码示例

return apply_filters( 'js_escape', $safe_text, $text );

📄 原文内容

Filters a string cleaned and escaped for output in JavaScript.

Description

Text passed to esc_js() is stripped of invalid or special characters, and properly slashed for output.

Parameters

$safe_textstring
The text after it has been escaped.
$textstring
The text prior to being escaped.

Source

return apply_filters( 'js_escape', $safe_text, $text );

Changelog

Version Description
2.0.6 Introduced.