Create Block Patterns Programmatically

Recently I’ve been implementing an automatic block pattern synchronisation feature into my Simple Multisite Crossposting plugin and decided why not to talk about it in a new post.

The goal of this tutorial is to learn how to create both synced and not synced block patterns programmatically and to assign specific pattern categories to them.

Before we dive deeply into the coding part I’d like to remind you what is the difference between synced and not synced patterns.

Block pattern typeHow it works exactly
Synced (former Reusable Blocks)When you insert patterns of this type into posts they are going to be inserted into post content like this: <!--wp:block {"ref":123} /-->. So, whenever you change the block pattern with ID 123 it will affect all the posts where it is used.
Not syncedWhen you use a pattern of this type in posts it is going to be inserted as blocks without any connection to the pattern itself. So you can make any changes in this pattern without worrying whether the changes are going to be applied anywhere else.

The interesting thing is that the whole difference between these types of patterns on the coding level is just a single meta value unsynced of the meta key wp_pattern_sync_status.

Ok, now we know everything, it is time to create one.

$block_pattern_id = wp_insert_post(
		'post_title' => 'Block Pattern by Misha',
		'post_content' => '<!-- wp:spacer {"height":"50px"} --><div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div><!-- /wp:spacer --><!-- wp:paragraph {"align":"center"} --><p class="has-text-align-center">Hello world</p><!-- /wp:paragraph --><!-- wp:spacer {"height":"50px"} --><div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div><!-- /wp:spacer -->',
		'post_name' => 'block-pattern-by-misha',
		'post_status' => 'publish',
		'comment_status' => 'closed',
		'ping_status' => 'closed',
		'post_type' => 'wp_block',

// you do not want to make it synced?
if( ! is_wp_error( $block_pattern_id ) ) {
	add_post_meta( $block_pattern_id, 'wp_pattern_sync_status', 'unsynced' );

A couple moments to keep in mind:

After using the code above our pattern will appear in “My Patterns”:

Creating a block pattern programmatically in WordPress

It consists just of a single paragraph and two “Spacer” blocks.

example of a block pattern

I also promised you to take a look at block pattern categories. What do you think they are? Right, they are just a custom taxonomy wp_pattern_category.

create block pattern categories programmatically

The nice thing is that your custom pattern categories are going to be displayed along with the other categories on the “Patterns” page in the site editor.

the list of block pattern categories in the site editor

All right, but how to create one? I think you can use wp_insert_term() to create a block pattern category programmatically and wp_set_object_terms() to assign it (or any of the existing categories) to a specific block pattern.

For example:

$block_pattern_category = wp_insert_term( 
	'My category', 
	array( 'slug' => 'my-cat' ) // it is the optional part

if( ! is_wp_error( $block_pattern_category ) ) {
	wp_set_object_terms( $block_pattern_id, 'my-cat', 'wp_pattern_category' );
	// or you can use $block_pattern_category[ 'term_id' ] instead of a slug
Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on X