Synced Patterns
Block Patterns are an excellent way to quickly add common components across our site. But what if we decide we want to update the language, link, or design used within that component? With standard patterns, we'd have to manually find and replace every use, this might mean dozens or even thousands of instances on a larger site.
This is where Synced Patterns shine. We can create a global component which can be edited once to then update across all instances where it is used. For example an info card, a grid of logos, or perhaps a simple call to action are all excellent candidates which may require such a feature. We will use the latter in our examples.
A simple Call to Action block pattern
One major caveat of using Synced Patterns is that they cannot be created programatically. A Synced Pattern is stored in the database and therefore would need to be manually created in each environment it is to be used. While any pattern you create can become synced, it is not possible to declare it as such in your theme or plugin.
We will discuss potential solutions for this problem below.
Creating a Synced Pattern
To mark a pattern as Synced, you would follow these steps:
- Select the block(s) you wish to sync and open the options dropdown along the right hand side of the List View or block toolbar.
- Choose "Create Pattern" from the dropdown.
- A modal window will then display where you can give your pattern a name and category, and most importantly, mark the pattern as Synced.
That's it! You can now find your component via the block inserter under the Patterns tab > My Patterns (and any other categories you declared in step 3).
The modal window for adding a new Pattern
Updating a Synced Pattern
To edit your Synced Pattern, you would do the following:
- Either navigate to it in your themes Patterns directory under the dashboard Appearance tab or select the pattern in a posts block editor window.
- Choose Edit Original in the block toolbar.
- On the next screen, edit your pattern as desired. Make a small text change, a major design overhaul, or even replace the component entirely with different blocks if you wish.
- Once completed, hit Save in the upper right.
- You can return to the page you were editing using the Back button located near the title in the upper center of the screen.
The block toolbar to edit your Synced Pattern
Creating Synced Patterns for Theme Authors
As mentioned above, Synced Patterns have to be manually set in each environment they are to be used.
This limitation somewhat complicates their use and while there is no one size fits all approach for how to best handle this, lets walk through one workflow that could make this easier on the developer experience for theme authors.
Ask yourself, is this the best approach?
Synced Patterns can be trickier to maintain and come with special considerations. Depending on your circumstances (budget, ability, time, etc...), it might be easier as a theme author to add a pattern in a block template part for use in your page templates. Or maybe you could create a simple custom block that renders your pattern instead.
If you need more flexibility, Synced Patterns can be a viable option to achive that end. Perhaps your end users are creatives and want the ability to make design changes to the component. Or you could be handing off the project to the client entirely and this gives them a bit more freedom in the long term.
It is up to you or your team to thoughtfully consider the trade offs to using Synced Patterns in your project.
1. Add the pattern in your theme
Add the pattern you wish to use to your themes patterns
directory. In the doc comment up top, set the Categories declaration to "hidden", and Inserter to "false".
<?php
/**
* Title: Call to Action
* Slug: tenup-theme/call-to-action
* Categories: hidden
* Inserter: false
*/
?>
<!-- wp:group {"metadata":{"name":"Call to Action"},"style":{"spacing":{"blockGap":"var:preset|spacing|10","padding":{"top":"var:preset|spacing|20","bottom":"var:preset|spacing|20","left":"var:preset|spacing|20","right":"var:preset|spacing|20"}}},"backgroundColor":"accent-4","layout":{"type":"flex","orientation":"vertical","justifyContent":"center"}} -->
<div class="wp-block-group has-accent-4-background-color has-background" style="padding-top:var(--wp--preset--spacing--20);padding-right:var(--wp--preset--spacing--20);padding-bottom:var(--wp--preset--spacing--20);padding-left:var(--wp--preset--spacing--20)">
<!-- wp:heading -->
<h2 class="wp-block-heading has-x-large-font-size">Want to get in touch?</h2>
<!-- /wp:heading -->
<!-- wp:paragraph -->
<p class="has-medium-font-size">We would love to hear from you!</p>
<!-- /wp:paragraph -->
<!-- wp:buttons -->
<div class="wp-block-buttons">
<!-- wp:button -->
<div class="wp-block-button">
<a class="wp-block-button__link wp-element-button">Contact Us</a>
</div>
<!-- /wp:button -->
</div>
<!-- /wp:buttons -->
</div>
<!-- /wp:group -->