函数文档

filter_block_kses_value()

💡 云策文档标注

概述

filter_block_kses_value() 函数用于过滤和清理解析后的块属性值,移除不允许的 HTML 内容。它支持递归处理数组值,并针对特定块类型(如 'core/template-part')进行特殊处理。

关键要点

  • 函数接受参数:$value(属性值,必需)、$allowed_html(允许的 HTML 元素和属性数组或上下文名称,必需)、$allowed_protocols(允许的 URL 协议数组,可选,默认 wp_allowed_protocols())、$block_context(块上下文数组,可选,默认 null)。
  • 返回值是过滤和清理后的字符串或数组,与输入类型一致。
  • 内部使用 wp_kses() 进行字符串清理,并递归处理数组值以保持结构。
  • 当 $block_context 包含 'blockName' 且为 'core/template-part' 时,会调用 filter_block_core_template_part_attributes() 进行额外属性处理。

代码示例

function filter_block_kses_value( $value, $allowed_html, $allowed_protocols = array(), $block_context = null ) {
    if ( is_array( $value ) ) {
        foreach ( $value as $key => $inner_value ) {
            $filtered_key   = filter_block_kses_value( $key, $allowed_html, $allowed_protocols, $block_context );
            $filtered_value = filter_block_kses_value( $inner_value, $allowed_html, $allowed_protocols, $block_context );

            if ( isset( $block_context['blockName'] ) && 'core/template-part' === $block_context['blockName'] ) {
                $filtered_value = filter_block_core_template_part_attributes( $filtered_value, $filtered_key, $allowed_html );
            }
            if ( $filtered_key !== $key ) {
                unset( $value[ $key ] );
            }

            $value[ $filtered_key ] = $filtered_value;
        }
    } elseif ( is_string( $value ) ) {
        return wp_kses( $value, $allowed_html, $allowed_protocols );
    }

    return $value;
}

注意事项

  • 函数从 WordPress 5.3.1 版本引入,6.5.5 版本添加了 $block_context 参数。
  • 相关函数包括 filter_block_core_template_part_attributes()、wp_kses() 和 filter_block_kses(),用于块属性清理和 HTML 过滤。

📄 原文内容

Filters and sanitizes a parsed block attribute value to remove non-allowable HTML.

Parameters

$valuestring[]|stringrequired
The attribute value 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[]optional
Array of allowed URL protocols.
Defaults to the result of wp_allowed_protocols() .

Default:array()

$block_contextarrayoptional
The block the attribute belongs to, in parsed block array format.

Default:null

Return

string[]|string The filtered and sanitized result.

Source

function filter_block_kses_value( $value, $allowed_html, $allowed_protocols = array(), $block_context = null ) {
	if ( is_array( $value ) ) {
		foreach ( $value as $key => $inner_value ) {
			$filtered_key   = filter_block_kses_value( $key, $allowed_html, $allowed_protocols, $block_context );
			$filtered_value = filter_block_kses_value( $inner_value, $allowed_html, $allowed_protocols, $block_context );

			if ( isset( $block_context['blockName'] ) && 'core/template-part' === $block_context['blockName'] ) {
				$filtered_value = filter_block_core_template_part_attributes( $filtered_value, $filtered_key, $allowed_html );
			}
			if ( $filtered_key !== $key ) {
				unset( $value[ $key ] );
			}

			$value[ $filtered_key ] = $filtered_value;
		}
	} elseif ( is_string( $value ) ) {
		return wp_kses( $value, $allowed_html, $allowed_protocols );
	}

	return $value;
}

Changelog

Version Description
6.5.5 Added the $block_context parameter.
5.3.1 Introduced.