函数文档

wp_pre_kses_less_than_callback()

💡 云策文档标注

概述

wp_pre_kses_less_than_callback() 是一个用于 preg_replace 的回调函数,主要用于在 HTML 转义前处理小于号字符,确保安全输出。

关键要点

  • 这是一个回调函数,设计用于 preg_replace 操作中。
  • 函数检查匹配字符串是否包含大于号,若不包含则使用 esc_html() 进行转义。
  • 参数 $matches 是一个字符串数组,由 preg_replace 的匹配结果填充。
  • 返回值是经过 esc_html() 处理后的文本或原始匹配字符串。

代码示例

function wp_pre_kses_less_than_callback( $matches ) {
    if ( ! str_contains( $matches[0], '>' ) ) {
        return esc_html( $matches[0] );
    }
    return $matches[0];
}

注意事项

  • 此函数自 WordPress 2.3.0 版本引入。
  • 与 esc_html() 函数相关,用于 HTML 块的转义。
  • 主要用于格式化处理,确保在 kses 过滤前正确处理小于号。

📄 原文内容

Callback function used by preg_replace.

Parameters

$matchesstring[]required
Populated by matches to preg_replace.

Return

string The text returned after esc_html if needed.

Source

function wp_pre_kses_less_than_callback( $matches ) {
	if ( ! str_contains( $matches[0], '>' ) ) {
		return esc_html( $matches[0] );
	}
	return $matches[0];
}

Changelog

Version Description
2.3.0 Introduced.