函数文档

_filter_block_template_part_area()

💡 云策文档标注

概述

_filter_block_template_part_area() 函数用于验证输入的模板部件区域名称是否为支持的值。如果支持则返回原值,否则返回未分类值并触发错误消息。

关键要点

  • 函数接收一个字符串参数 $type,表示模板部件区域名称。
  • 通过 get_allowed_block_template_part_areas() 获取允许的区域列表,并检查输入是否在列表中。
  • 如果输入不被支持,函数会触发一个用户级错误消息,并返回 WP_TEMPLATE_PART_AREA_UNCATEGORIZED 常量值。
  • 此函数在 WordPress 5.9.0 版本中引入。

代码示例

function _filter_block_template_part_area( $type ) {
    $allowed_areas = array_map(
        static function ( $item ) {
            return $item['area'];
        },
        get_allowed_block_template_part_areas()
    );
    if ( in_array( $type, $allowed_areas, true ) ) {
        return $type;
    }

    $warning_message = sprintf(
        /* translators: %1$s: Template area type, %2$s: the uncategorized template area value. */
        __( '"%1$s" is not a supported wp_template_part area value and has been added as "%2$s".' ),
        $type,
        WP_TEMPLATE_PART_AREA_UNCATEGORIZED
    );
    wp_trigger_error( __FUNCTION__, $warning_message );
    return WP_TEMPLATE_PART_AREA_UNCATEGORIZED;
}

📄 原文内容

Checks whether the input ‘area’ is a supported value.

Description

Returns the input if supported, otherwise returns the ‘uncategorized’ value.

Parameters

$typestringrequired
Template part area name.

Return

string Input if supported, else the uncategorized value.

Source

function _filter_block_template_part_area( $type ) {
	$allowed_areas = array_map(
		static function ( $item ) {
			return $item['area'];
		},
		get_allowed_block_template_part_areas()
	);
	if ( in_array( $type, $allowed_areas, true ) ) {
		return $type;
	}

	$warning_message = sprintf(
		/* translators: %1$s: Template area type, %2$s: the uncategorized template area value. */
		__( '"%1$s" is not a supported wp_template_part area value and has been added as "%2$s".' ),
		$type,
		WP_TEMPLATE_PART_AREA_UNCATEGORIZED
	);
	wp_trigger_error( __FUNCTION__, $warning_message );
	return WP_TEMPLATE_PART_AREA_UNCATEGORIZED;
}

Changelog

Version Description
5.9.0 Introduced.