I think what you want is Block Patterns, this post will tell you how to make them: https://richtabor.com/build-block-patterns/
Yes it’s patterns I want to make, that’s what I was meaning when I was referring to templates. Mixing the words up.
Creating them with code isn’t good enough because we’ll be making quite a few of them routinely and they back up with me to add the code.
As I said above, reusable blocks don’t work because they are ‘one block’ when used across the site. If you change it, it’s reflected across all the locations unless you convert it back into blocks.
Unless you know a way to stop this?
What I need is patterns but being able to create them from the editor. I suspect I’m going to need to make a plugin for it.
As I said above, reusable blocks don’t work because they are ‘one block’ when used across the site. If you change it, it’s reflected across all the locations unless you convert it back into blocks.
Ah, sorry I jumped ahead, or behind, or nowhere useful at least, and didn’t realize you needed to tweak them.
What I need is patterns but being able to create them from the editor. I suspect I’m going to need to make a plugin for it.
Agree. I don’t think it’d have to be a very large plugin, but it’d help manage a growing library of patterns and ensure consistency.
https://developer.wordpress.org/block-editor/reference-guides/block-api/block-patterns/#register_block_pattern
Good luck with it!
I’m actually building this right now and it seems to work.
I’ve got some questions though:
1) It looks like the register_block_pattern() function only works during init. Am I right with that?
If so, is it appropriate for me to do be doing a db call during every init cycle to grab the published post types I’ve created for them to them to call this function? It feels a bit ugly.
2) I’m making an assumption that register_block_pattern is something that needs to be called every load for each pattern and that they doesn’t persist. (I.e. I can’t just call it once and the block will stay available forever via some DB entry or something?). Is that right?
It looks like the register_block_pattern() function only works during init. Am I right with that?
Yes, that’s what the documentation recommends.
Though within a custom plugin, I’ve also registered and unregistered patterns in the current_screen
and plugins_loaded
hooks.
Sometimes you’ll need $current_screen
for example. I can’t say if that’s 100% legit, but it worked for me.
See also: https://github.com/WordPress/gutenberg/blob/release/11.4/lib/block-patterns.php#L251
I’m making an assumption that register_block_pattern is something that needs to be called every load for each pattern and that they doesn’t persist.
I’m pretty sure your assumption is correct here.
When we register a pattern, they’re loaded every page refresh.
I think I’m right in saying that we build the patterns registry every time (maybe PHP does some optimisation in the background), and we don’t cache them in transients etc like we do the patterns directory request for example.
-
This reply was modified 2 years, 7 months ago by ramonopoly. Reason: formatting
Thanks for replying 🙂
Well, it works now! Adds a custom post type called “Patterns”. When you add one, it appears in the pattern directory. If you give it a category name, it uses that in the patterns dropdown, or defaults to “Custom Patterns”.
I just need to decide if I can be bothered to tidy it up and submit it! It’s been a long time since I had a plugin published…