register_block_pattern()
云策文档标注
概述
register_block_pattern() 函数用于在 WordPress 中注册一个新的块模式,通过 WP_Block_Patterns_Registry 类实现。它接受模式名称和属性数组作为参数,返回布尔值表示注册是否成功。
关键要点
- 函数参数:$pattern_name(必需,包含命名空间的块模式名称)和 $pattern_properties(必需,属性数组,参考 WP_Block_Patterns_Registry::register())。
- 返回值:布尔值,成功注册返回 true,否则返回 false。
- 内部实现:调用 WP_Block_Patterns_Registry::get_instance()->register() 方法。
- 最佳实践:建议在 init 钩子中调用 register_block_pattern(),以避免编辑器错误。
- 属性数组包括 title(必需)、content(必需)、description、categories、keywords 和 viewportWidth 等字段。
- 块模式可以分配到多个类别,使用数组指定 categories。
- 从 WordPress 6.0 开始,块主题可以通过 /patterns 子文件夹中的 PHP 文件注册模式。
- 移除核心块模式时,确保至少注册一个块模式类别,防止编辑器崩溃。
代码示例
register_block_pattern(
'wpdocs-my-plugin/my-awesome-pattern',
array(
'title' => __( 'Two buttons', 'wpdocs-my-plugin' ),
'description' => _x( 'Two horizontal buttons, the left button is filled in, and the right button is outlined.', 'Block pattern description', 'wpdocs-my-plugin' ),
'content' => "<!-- wp:buttons {"align":"center"} -->n<div class="wp-block-buttons aligncenter"><!-- wp:button {"backgroundColor":"very-dark-gray","borderRadius":0} -->n<div class="wp-block-button"><a class="wp-block-button__link has-background has-very-dark-gray-background-color no-border-radius">" . esc_html__( 'Button One', 'wpdocs-my-plugin' ) . "</a></div>n<!-- /wp:button -->nn<!-- wp:button {"textColor":"very-dark-gray","borderRadius":0,"className":"is-style-outline"} -->n<div class="wp-block-button is-style-outline"><a class="wp-block-button__link has-text-color has-very-dark-gray-color no-border-radius">" . esc_html__( 'Button Two', 'wpdocs-my-plugin' ) . "</a></div>n<!-- /wp:button --></div>n<!-- /wp:buttons -->",
'categories' => ['custom_category', 'columns'],
)
);注意事项
- 注册块模式时,应使用 init 钩子,避免直接从 functions.php 调用导致编辑器错误。
- 确保属性数组中的 title 和 content 为必需字段,description 虽可选但推荐提供。
- 使用 remove_theme_support('core-block-patterns') 移除核心模式时,需注册至少一个类别。
原文内容
Registers a new block pattern.
Parameters
$pattern_namestringrequired-
Block pattern name including namespace.
$pattern_propertiesarrayrequired-
List of properties for the block pattern.
See WP_Block_Patterns_Registry::register() for accepted arguments.
Source
function register_block_pattern( $pattern_name, $pattern_properties ) {
return WP_Block_Patterns_Registry::get_instance()->register( $pattern_name, $pattern_properties );
}
Changelog
| Version | Description |
|---|---|
| 5.5.0 | Introduced. |
Skip to note 6 content
Christina Blust
Per the Block Patterns documentation in the Block Editor Handbook, the $pattern_properties array includes:
and the example function given is:
register_block_pattern( 'wpdocs-my-plugin/my-awesome-pattern', array( 'title' => __( 'Two buttons', 'wpdocs-my-plugin' ), 'description' => _x( 'Two horizontal buttons, the left button is filled in, and the right button is outlined.', 'Block pattern description', 'wpdocs-my-plugin' ), 'content' => "<!-- wp:buttons {"align":"center"} -->n<div class="wp-block-buttons aligncenter"><!-- wp:button {"backgroundColor":"very-dark-gray","borderRadius":0} -->n<div class="wp-block-button"><a class="wp-block-button__link has-background has-very-dark-gray-background-color no-border-radius">" . esc_html__( 'Button One', 'wpdocs-my-plugin' ) . "</a></div>n<!-- /wp:button -->nn<!-- wp:button {"textColor":"very-dark-gray","borderRadius":0,"className":"is-style-outline"} -->n<div class="wp-block-button is-style-outline"><a class="wp-block-button__link has-text-color has-very-dark-gray-color no-border-radius">" . esc_html__( 'Button Two', 'wpdocs-my-plugin' ) . "</a></div>n<!-- /wp:button --></div>n<!-- /wp:buttons -->", ) );register_block_pattern( 'wpdocs-my-plugin/my-awesome-pattern', array( 'title' => __( 'Two buttons', 'wpdocs-my-plugin' ), 'description' => _x( 'Two horizontal buttons, the left button is filled in, and the right button is outlined.', 'Block pattern description', 'wpdocs-my-plugin' ), 'content' => "<!-- wp:buttons {"align":"center"} -->n<div class="wp-block-buttons aligncenter"><!-- wp:button {"backgroundColor":"very-dark-gray","borderRadius":0} -->n<div class="wp-block-button"><a class="wp-block-button__link has-background has-very-dark-gray-background-color no-border-radius">" . esc_html__( 'Button One', 'wpdocs-my-plugin' ) . "</a></div>n<!-- /wp:button -->nn<!-- wp:button {"textColor":"very-dark-gray","borderRadius":0,"className":"is-style-outline"} -->n<div class="wp-block-button is-style-outline"><a class="wp-block-button__link has-text-color has-very-dark-gray-color no-border-radius">" . esc_html__( 'Button Two', 'wpdocs-my-plugin' ) . "</a></div>n<!-- /wp:button --></div>n<!-- /wp:buttons -->", 'categories' => ['custom_category', 'columns'], ) );how to define/register a category can be found here: https://developer.wordpress.org/reference/functions/register_block_pattern_category/
Skip to note 7 content
Marcio Duarte
Just a heads up for block theme developers: since WordPress 6.0 you can also register patterns in a block theme simply by placing PHP files with patterns in your theme’s
/patternssubfolder.Skip to note 8 content
mikehealy
The docs & handbook don’t seem to mention it, but I gather
register_block_pattern()should be called from a handler attached to theinithook.function wpdocs_register_my_patterns() { register_block_pattern( ... ); } add_action( 'init', 'wpdocs_register_my_patterns' );register_block_patterninside the init hook solves this perfectly! Thanks!Skip to note 9 content
tomepajk
If the core block patterns are removed via:
remove_theme_support( 'core-block-patterns' );Make sure that there is at least one block pattern category registered. The block editor crashes if none are present.
Skip to note 10 content
thejaydip
A basic example of how to register a new pattern block.
function wpdocs_register_block_patterns() { register_block_pattern( 'wpdocs/my-example', array( 'title' => __( 'My First Block Pattern', 'textdomain' ), 'description' => _x( 'This is my first block pattern', 'Block pattern description', 'textdomain' ), 'content' => '<!-- wp:paragraph --><p>A single paragraph block style</p><!-- /wp:paragraph -->', 'categories' => array( 'text' ), 'keywords' => array( 'cta', 'demo', 'example' ), 'viewportWidth' => 800, ) ); } add_action( 'init', 'wpdocs_register_block_patterns' );