get_block_bindings_supported_attributes()
云策文档标注
概述
get_block_bindings_supported_attributes() 函数用于检索块绑定支持的块属性列表。它接受一个块类型参数,返回该块类型支持的属性数组,并可通过过滤器进行自定义。
关键要点
- 函数参数:$block_type(字符串,必需),指定要检索支持属性的块类型。
- 返回值:数组,包含块绑定支持的属性列表。
- 内置支持:函数预定义了多个核心块(如 core/paragraph、core/image)及其支持的属性。
- 过滤器:提供 block_bindings_supported_attributes 和 block_bindings_supported_attributes_{$block_type} 两个过滤器,允许开发者修改支持的属性。
- 版本:自 WordPress 6.9.0 引入。
代码示例
function get_block_bindings_supported_attributes( $block_type ) {
$block_bindings_supported_attributes = array(
'core/paragraph' => array( 'content' ),
'core/heading' => array( 'content' ),
'core/image' => array( 'id', 'url', 'title', 'alt', 'caption' ),
'core/button' => array( 'url', 'text', 'linkTarget', 'rel' ),
'core/post-date' => array( 'datetime' ),
'core/navigation-link' => array( 'url' ),
'core/navigation-submenu' => array( 'url' ),
);
$supported_block_attributes =
isset( $block_type, $block_bindings_supported_attributes[ $block_type ] ) ?
$block_bindings_supported_attributes[ $block_type ] :
array();
$supported_block_attributes = apply_filters(
'block_bindings_supported_attributes',
$supported_block_attributes,
$block_type
);
$supported_block_attributes = apply_filters(
"block_bindings_supported_attributes_{$block_type}",
$supported_block_attributes
);
return $supported_block_attributes;
}注意事项
- 如果块类型未在预定义数组中,函数返回空数组。
- 过滤器允许动态调整支持的属性,增强灵活性。
- 相关函数:get_block_editor_settings() 使用此函数获取块编辑器设置。
原文内容
Retrieves the list of block attributes supported by block bindings.
Parameters
$block_typestringrequired-
The block type whose supported attributes are being retrieved.
Source
function get_block_bindings_supported_attributes( $block_type ) {
$block_bindings_supported_attributes = array(
'core/paragraph' => array( 'content' ),
'core/heading' => array( 'content' ),
'core/image' => array( 'id', 'url', 'title', 'alt', 'caption' ),
'core/button' => array( 'url', 'text', 'linkTarget', 'rel' ),
'core/post-date' => array( 'datetime' ),
'core/navigation-link' => array( 'url' ),
'core/navigation-submenu' => array( 'url' ),
);
$supported_block_attributes =
isset( $block_type, $block_bindings_supported_attributes[ $block_type ] ) ?
$block_bindings_supported_attributes[ $block_type ] :
array();
/**
* Filters the supported block attributes for block bindings.
*
* @since 6.9.0
*
* @param string[] $supported_block_attributes The block's attributes that are supported by block bindings.
* @param string $block_type The block type whose attributes are being filtered.
*/
$supported_block_attributes = apply_filters(
'block_bindings_supported_attributes',
$supported_block_attributes,
$block_type
);
/**
* Filters the supported block attributes for block bindings.
*
* The dynamic portion of the hook name, `$block_type`, refers to the block type
* whose attributes are being filtered.
*
* @since 6.9.0
*
* @param string[] $supported_block_attributes The block's attributes that are supported by block bindings.
*/
$supported_block_attributes = apply_filters(
"block_bindings_supported_attributes_{$block_type}",
$supported_block_attributes
);
return $supported_block_attributes;
}
Hooks
- apply_filters( ‘block_bindings_supported_attributes’, string[] $supported_block_attributes, string $block_type )
-
Filters the supported block attributes for block bindings.
- apply_filters( “block_bindings_supported_attributes_{$block_type}”, string[] $supported_block_attributes )
-
Filters the supported block attributes for block bindings.
Changelog
| Version | Description |
|---|---|
| 6.9.0 | Introduced. |