We’ve been bundling block patterns with all of our themes for quite a while now, but recent improvements to the Template Part and Query Loop blocks has opened up a whole new world for how patterns can be used.
Both blocks can now display a carousel of block patterns in their setup state, allowing users to choose between a menu of pre-designed versions of these blocks. These patterns can be bundled with your theme, so that users have a wide range of pre-designed header, query, and footer options available to choose from in the Site Editor:
The only difference between these and other patterns you’d build is that they include a special
blockTypes property to let Gutenberg know which block to associate them with. Here’s a simplified example:
register_block_pattern( 'theme/pattern-slug', array( 'title' => _x( 'Pattern Title' 'theme-slug' ), 'blockTypes' => array( 'core/template-part/header' ), 'content' => '<!-- Your Pattern Markup -->' ) );
The markup above specifies that this pattern should be surfaced for a Header template part block. The value there would
core/query if you were designing a pattern for those blocks instead. (This property also powers pattern transforms, but that’s a topic for another time).
Once you register a pattern that way, it will show up in the carousel for the specified block. It’s as simple as that!
There are already a few Query Loop patterns bundled with Core today that use this method, and the header patterns shown in the video above are actually from a Gutenberg PR I’m working on as well. I’ve prepared a quick code example using our in-progress Quadrat theme to show how they could be bundled — we definitely plan on including a handful of these sorts of patterns in our block themes moving forward.
When combined together, Header, Query Loop, and Footer patterns allow you to mix-and-match pre-designed sections to create your entire website. I’m truly excited to see not only how themes use this feature, but how the Pattern Directory might provide hundreds more options for these blocks. This feels groundbreaking for WordPress, and I can’t wait to see how it turns out.