函数文档

wp_specialchars()

💡 云策文档标注

概述

wp_specialchars() 是一个已弃用的 WordPress 函数,用于对 HTML 块进行转义,自 2.8.0 版本起被 esc_html() 替代。它处理特殊字符的 HTML 实体转换,但建议开发者使用 esc_html() 以保持代码更新。

关键要点

  • wp_specialchars() 是已弃用函数,自 WordPress 2.8.0 起被 esc_html() 取代。
  • 函数接受参数 $text(必需)用于转义文本,其他参数如 $quote_style、$charset 和 $double_encode 已弃用或未使用。
  • 如果传递多个参数,函数会调用 _wp_specialchars() 以保持向后兼容;否则直接返回 esc_html($text)。
  • 相关函数包括 _wp_specialchars()、esc_html() 和 _deprecated_function()。

代码示例

function wp_specialchars( $text, $quote_style = ENT_NOQUOTES, $charset = false, $double_encode = false ) {
    _deprecated_function( __FUNCTION__, '2.8.0', 'esc_html()' );
    if ( func_num_args() > 1 ) { // Maintain back-compat for people passing additional arguments.
        return _wp_specialchars( $text, $quote_style, $charset, $double_encode );
    } else {
        return esc_html( $text );
    }
}

注意事项

开发者应避免使用 wp_specialchars(),转而使用 esc_html() 以确保代码兼容性和安全性,因为此函数已被标记为弃用。


📄 原文内容

Legacy escaping for HTML blocks.

Description

See also

Parameters

$textstringrequired
Text to escape.
$quote_stylestringoptional
Unused.

Default:ENT_NOQUOTES

$charsetfalse|stringoptional
Unused.

Default:false

$double_encodefalseoptional
Whether to double encode. Unused.

Default:false

Return

string Escaped $text.

Source

function wp_specialchars( $text, $quote_style = ENT_NOQUOTES, $charset = false, $double_encode = false ) {
	_deprecated_function( __FUNCTION__, '2.8.0', 'esc_html()' );
	if ( func_num_args() > 1 ) { // Maintain back-compat for people passing additional arguments.
		return _wp_specialchars( $text, $quote_style, $charset, $double_encode );
	} else {
		return esc_html( $text );
	}
}

Changelog

Version Description
2.8.0 Introduced.