函数文档

wp_kses_split()

💡 云策文档标注

概述

wp_kses_split() 是 WordPress 中用于搜索和修复 HTML 标签的函数,无论其格式是否正确。它通过正则表达式匹配 HTML 标记,并调用回调函数处理,以过滤内容中的不安全 HTML。

关键要点

  • 函数搜索 HTML 标签,包括格式错误的标签和杂散的 > 字符。
  • 接受三个参数:$content(要过滤的内容)、$allowed_html(允许的 HTML 元素和属性数组或上下文名称)和 $allowed_protocols(允许的 URL 协议数组)。
  • 返回修复后的 HTML 标签内容字符串。
  • 内部使用 preg_replace_callback 和正则表达式模式匹配,并调用 _wp_kses_split_callback 处理。
  • 与 wp_kses() 相关,用于过滤文本内容并移除不允许的 HTML。
  • 自版本 1.0.0 引入,6.6.0 版本增强了对无效 HTML 转换为注释的识别。

代码示例

function wp_kses_split( $content, $allowed_html, $allowed_protocols ) {
	global $pass_allowed_html, $pass_allowed_protocols;

	$pass_allowed_html      = $allowed_html;
	$pass_allowed_protocols = $allowed_protocols;

	$token_pattern = <<<REGEX
~(
	(?:&(?:[a-z0-9]+|#d+|#x[a-f0-9]+);)  #  - HTML 实体。
	|
	(?:<!(?:--.*?--s*)+>)                 #  - 规范 HTML 注释。
	|
	(?:</[^s/>]*s*>)                  #  - 带有无效标签名的闭合标签。
	|
	(?:<[^s>]*s*>)                       #  - 无效标记声明节点。并非所有无效节点
	                                       #    都匹配,以避免破坏遗留行为。
)
|
([^<]*(?:<(?!w+b|/[^>])[^<]*)*)      # 类似标签的文本跨度。
~x
REGEX;
	return preg_replace_callback( $token_pattern, '_wp_kses_split_callback', $content );
}

📄 原文内容

Searches for HTML tags, no matter how malformed.

Description

It also matches stray > characters.

Parameters

$contentstringrequired
Content to filter.
$allowed_htmlarray[]|stringrequired
An array of allowed HTML elements and attributes, or a context name such as 'post'. See wp_kses_allowed_html() for the list of accepted context names.
$allowed_protocolsstring[]required
Array of allowed URL protocols.

Return

string Content with fixed HTML tags

Source

function wp_kses_split( $content, $allowed_html, $allowed_protocols ) {
	global $pass_allowed_html, $pass_allowed_protocols;

	$pass_allowed_html      = $allowed_html;
	$pass_allowed_protocols = $allowed_protocols;

	$token_pattern = <<<REGEX
~
	(                      # Detect comments of various flavors before attempting to find tags.
		(<!--.*?(-->|$))   #  - Normative HTML comments.
		|
		<!--[^a-zA-Z][^-->]*>  #  - Closing tags with invalid tag names.
		|
		<!--[^-->]*>           #  - Invalid markup declaration nodes. Not all invalid nodes
		                   #    are matched so as to avoid breaking legacy behaviors.
	)
	|
	(<[^>]*(>|$)|>)        # Tag-like spans of text.
~x
REGEX;
	return preg_replace_callback( $token_pattern, '_wp_kses_split_callback', $content );
}

Changelog

Version Description
6.6.0 Recognize additional forms of invalid HTML which convert into comments.
1.0.0 Introduced.

User Contributed Notes