1

I'm working on a large web site with many custom post types and many block patterns, and each cpt has a suggested set & order of the block patterns. For my needs, the block template functionality is great as is, however its API is obtuse (it's time consuming to translate classNames, attributes, and placeholder text to its php array markup) and meanwhile I already have block patterns. So, is it possible to insert block patterns via the block templates?

Alternatively, where is the API for block patterns defined? Looking for a reference similar to insertBlock but for patterns.

1
  • I wrote an answer but it's difficult to know if I sent you down the right path because you provided no context, it's not clear what you're trying to achieve that requires this. Patterns aren't something you insert, patterns are just predefined blocks, it's the blocks not the pattern that get inserted. It has more in common with copy paste
    – Tom J Nowell
    Commented Sep 12, 2021 at 20:59

1 Answer 1

2

but it looks like that's not possible?

Correct, it would not make sense to be able to do so as patterns are just predefined snippets of block HTML. They aren't entities/blocks/objects themselves.

Block templates are placeholder blocks, a suggestion for the user that can either be locked in place so they can't be changed, or just the starting child blocks.

A pattern is like copy pasting some content you saw on a site of examples.

A block template is the what you get before the user has inserted data for a post types content, or a blocks inner blocks. Think of it like that initial paragraph block that tells you to start writing here.

Variants are when your block has an attribute and you display the block multiple times but each registered variant has a different value for the attribute that can have a different name/icon, e.g. all of the core embeds are a single block that changes based on what it's embedding.

Variations in newer versions of the block editor can be shown as a selection for the user when the block is first added. This is how the query block asks the user how they want to display content. These variants use the innerBlocks: [ field to change the content of the blocks once the user has selected a variant. If your goal is to allow the user to select a predefined set of blocks for inner blocks, then this is the closest you will find


Update: Since the answer was written, a core pattern block was added, but it's not intended for the purpose that was asked. It's main purpose is as a workaround in the default themes to reference images in the theme in content, and load translation strings. It is not a solution to the OP's question.

1
  • Thanks! I added context. Yes block patterns are effectively just a copy/paste – but I'd think many a theme would find it very useful to insert a pattern in specific contexts, for example in a custom post type or, say, if you select a format or term. I should look into variants / variations, though as this is for the whole post sounds not ideal.
    – spnzr
    Commented Sep 13, 2021 at 22:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.