register_block_pattern_category()
云策文档标注
概述
register_block_pattern_category() 函数用于在 WordPress 中注册一个新的区块模式分类。它接受分类名称和属性数组作为参数,并返回布尔值表示注册是否成功。
关键要点
- 函数参数:$category_name(字符串,必需,包含命名空间的模式分类名称)和 $category_properties(数组,必需,区块模式的属性列表,具体参数参考 WP_Block_Pattern_Categories_Registry::register())。
- 返回值:布尔值,成功注册返回 true,否则返回 false。
- 内部实现:函数调用 WP_Block_Pattern_Categories_Registry::get_instance()->register() 来执行注册。
- 引入版本:WordPress 5.5.0。
- 相关函数:_register_core_block_patterns_and_categories() 用于注册核心区块模式和分类。
代码示例
register_block_pattern_category(
'hero',
array( 'label' => __( 'Hero', 'wpdocs-my-plugin' ) )
);注意事项
- 建议在 init hook 中调用 register_block_pattern_category(),以确保正确注册。
- 核心已存在的分类包括:buttons、columns、gallery、header、text、featured 和 query。
- 可以通过 wp.data.select('core').getBlockPatternCategories() 在 JavaScript 中获取所有已注册的分类列表。
原文内容
Registers a new pattern category.
Parameters
$category_namestringrequired-
Pattern category name including namespace.
$category_propertiesarrayrequired-
List of properties for the block pattern.
See WP_Block_Pattern_Categories_Registry::register() for accepted arguments.
Source
function register_block_pattern_category( $category_name, $category_properties ) {
return WP_Block_Pattern_Categories_Registry::get_instance()->register( $category_name, $category_properties );
}
Changelog
| Version | Description |
|---|---|
| 5.5.0 | Introduced. |
Skip to note 5 content
Felipe Lavín
To get the list of all registered categories, paste into the js inspector:
wp.data.select('core').getBlockPatternCategories()Skip to note 6 content
Christina Blust
Example from Block Editor Handbook:
register_block_pattern_category( 'hero', array( 'label' => __( 'Hero', 'wpdocs-my-plugin' ) ) );These categories already exist in core as of 5.5.1:
Skip to note 7 content
Sajjad Hussain
Another way to register custom block category with init hook
function wpdocs_block_pattern_category() { register_block_pattern_category( 'wpdocs-patterns', array( 'label' => __( 'My Patterns', 'text-domain' ) ) ); } add_action( 'init', 'wpdocs_block_pattern_category' );Skip to note 8 content
Lovro Hrust
From Block editor handbook:
register_block_pattern_category()should be called from a handler attached to the init hook.