函数文档

wp_filter_kses()

💡 云策文档标注

概述

wp_filter_kses() 是一个 WordPress 函数,用于根据允许的 HTML KSES 规则清理内容,特别适用于已转义斜杠的数据。

关键要点

  • 函数期望输入数据已转义斜杠,并返回过滤后的内容字符串。
  • 在钩子系统中,wp_filter_kses 通常比 wp_kses_data 更受推荐,因为它能更早地处理 $_GET、$_POST 等数据。
  • 函数内部通过 stripslashes() 去除斜杠,使用 wp_kses() 过滤,再通过 addslashes() 重新转义。

代码示例

function wp_filter_kses( $data ) {
	return addslashes( wp_kses( stripslashes( $data ), current_filter() ) );
}

注意事项

  • 确保传入的数据已正确转义斜杠,以避免安全风险。
  • 相关函数包括 wp_kses() 用于过滤文本内容,current_filter() 用于获取当前钩子名称。

📄 原文内容

Sanitize content with allowed HTML KSES rules.

Description

This function expects slashed data.

Parameters

$datastringrequired
Content to filter, expected to be escaped with slashes.

Return

string Filtered content.

More Information

wp_filter_kses should generally be preferred over wp_kses_data because wp_magic_quotes escapes $_GET, $_POST, $_COOKIE, $_SERVER, and $_REQUEST fairly early in the hook system, shortly after ‘plugins_loaded’ but earlier then ‘init’ or ‘wp_loaded’.

Source

function wp_filter_kses( $data ) {
	return addslashes( wp_kses( stripslashes( $data ), current_filter() ) );
}

Changelog

Version Description
1.0.0 Introduced.