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 type | How 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 synced | When 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(
array(
'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:
- Yes, we used the WordPress function
wp_insert_post(), which allows to create any type of posts programmatically (well, maybe except WooCommerce orders and products). - The
post_contentcontains some kind of HTML – where can we get that? Well, you need to create a pattern using the Block Editor anyway and then copy the code or just get it from another block pattern usingget_post()function. - And as you can see block patterns are just a custom post type
wp_block.
After using the code above our pattern will appear in “My Patterns”:

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

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.

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.

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',
'wp_pattern_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
Hey guys and welcome to my website. For more than 15 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
Nice and neet explanation. I am curious to know why not we can use Patterns API like
register_block_pattern,register_block_pattern_category. Please correct me if I misunderstood something. Thanks.Thank you, I was re-reading this post today and got kind of similar question for a moment, but just for a moment ;)
The idea here is to actually create a pattern in the database, because this post was created when the appropriate functionality related to crossposting patterns was requested as part of my plugin.
So, our goal is to create a pattern, but not to register it in the theme.
Misha, thanks for the reply. Now I understand the actual implementation.
good
Nice explanation, thank you for sharing.
I wonder if I can use an existing custom block pattern (created via code or via backoffice) in my code, like a template for a custom query loop.
Thanks again!