tools/block-pattern-generator
WordPress Block Pattern Generator
Register a block pattern with register_block_pattern(), or ship it as a file in your theme's patterns folder — the same pattern, written the way each of the two expects it.
snippet.php
/**
* Registers the 'Hero' block pattern.
*
* On 'init', which is where the inserter looks for patterns. A pattern is block
* markup plus the metadata the inserter files it under; it is copied into the
* post when someone chooses it, so editing this later does not change the posts
* that already used it.
*
* A category named here that nobody registered is not an error. Verified on
* WordPress 7.1: the pattern still registers and still appears over the REST
* API with that category on it, the category is not created, and the inserter
* silently has nowhere to file it. register_block_pattern_category() is the
* call that creates one.
*/
function wps_register_block_patterns() {
register_block_pattern(
'my-plugin/hero',
array(
'title' => __( 'Hero', 'text-domain' ),
'description' => _x( 'A heading and a paragraph, centred.', 'Block pattern description', 'text-domain' ),
'categories' => array( 'featured' ),
'inserter' => true,
'content' => '<!-- wp:group {"layout":{"type":"constrained"}} -->
<div class="wp-block-group">
<!-- wp:heading {"textAlign":"center","level":1} -->
<h1 class="wp-block-heading has-text-align-center">Hello</h1>
<!-- /wp:heading -->
<!-- wp:paragraph {"align":"center"} -->
<p class="has-text-align-center">A line of text under the heading.</p>
<!-- /wp:paragraph -->
</div>
<!-- /wp:group -->',
)
);
}
add_action( 'init', 'wps_register_block_patterns' );