Skip to content
wp-skills
Register
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.

A PHP call works from a plugin or a theme. A patterns/ file needs no call at all and exists only while that theme is active.

Patterns share one registry, so the name is namespaced the way a block's is.

Required. WordPress registers nothing at all without it, and says so only in a notice nobody sees on a live site.

Comma separated. A category nobody registered is not an error — the pattern registers and the inserter quietly has nowhere to file it.

Comma separated search terms.

Comma separated. Offers the pattern when one of these blocks is being placed — core/post-content, say.

Comma separated. Limits where the pattern is offered.

The width the inserter draws the preview at, in pixels.

Off hides it from the pattern list — for one that is only ever placed by a template or by blockTypes.

register_block_pattern_category(), in the same function and before the pattern.

Required. Copy it out of the editor's code view — Options, then Code editor — so the delimiters match what the parser expects.

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' );