wp_filter_post_kses()
云策文档标注
概述
wp_filter_post_kses() 函数用于对文章内容进行 HTML 标签过滤,确保只保留允许的 HTML 标签和属性。它处理的是 'post' 类型的内容,而非表单 $_POST 数据,并期望输入数据已转义。
关键要点
- 函数用途:清理文章内容中的 HTML,基于 wp_kses() 实现,使用 'post' 上下文定义允许的标签。
- 参数要求:输入 $data 应为字符串,且已转义(带反斜杠),函数内部会先去除转义再过滤。
- 返回值:返回过滤后的字符串,保留允许的 HTML 标签和属性,并重新转义。
- 相关函数:与 wp_kses() 紧密相关,后者执行实际的 HTML 过滤逻辑。
代码示例
function wp_filter_post_kses( $data ) {
return addslashes( wp_kses( stripslashes( $data ), 'post' ) );
}注意事项
- 注意:此函数专用于 'post' 类型内容,不适用于其他数据源如表单提交。
- 用户贡献笔记指出,函数内部调用 wp_kses() 时传递的第二个参数是字符串 'post',而 wp_kses() 文档显示该参数应为数组,这可能是一个文档或实现上的不一致点,开发者使用时需留意。
原文内容
Sanitizes content for allowed HTML tags for post content.
Description
Post content refers to the page contents of the ‘post’ type and not $_POST data from forms.
This function expects slashed data.
Parameters
$datastringrequired-
Post content to filter, expected to be escaped with slashes.
Source
function wp_filter_post_kses( $data ) {
return addslashes( wp_kses( stripslashes( $data ), 'post' ) );
}
Changelog
| Version | Description |
|---|---|
| 2.0.0 | Introduced. |
Skip to note 2 content
Benjiwp
The second argument passes to kses() does not match with its documentation :
wp_kses( string $string, array $allowed_html, array $allowed_protocols = array() )https://developer.wordpress.org/reference/functions/wp_kses/