While working on my Gutenberg Development Course, one of the hardest things I found to research was how to add block templates to your WordPress theme or plugin.
Example of a simple template with image and paragraph block
Block templates are a way to have certain blocks show up by default for a new post, page or custom post type. You can also “lock” the template to prevent users from adding or removing blocks.
How Block Templates Work
Templates are added as a parameter when registering a post type in WordPress. Sometimes this is done for your own custom post type. But it can also be done for existing and native post types like Posts and Pages.
The block template setting itself is an array of blocks that you want included. There is also an additional setting called “template_lock” that can be set to true or false to lock down the template to users.
You can also add additional configurations like custom placeholder text for blocks or set the alignment.
An Example of Block Templates
You can add the code below to any plugin or theme to modify template setting for post types of your choice.
Some notes on this code:
- Notice the conditional statement determining what post type to apply this to. Can change for your own needs.
- The template_lock argument determines whether users can add or remove blocks with this post type.
- The name used for blocks is the name used when creating them. See a list here of all default blocks, which you can use to lookup (or guess) the programatic name for a block.
- You can add additional configurations like placeholder text, alignment, or other block attributes you want set by default.
Learn More About Gutenberg Development
To learn more about developing with blocks in WordPress, please check out my course Gutenberg Development Course.
A couple weeks ago I had a chance to present on PHP for WordPress at WordCamp Baltimore 2015. Thanks to the good folks who recorded and got everything together to post on WordCamp.tv you can now watch the video of the talk.
Check it out and then head over and learn more about my PHP for WordPress Course at Treehouse 🙂https://videopress.com/videopress-iframe.js
I’m very excited to be back at a WordCamp in Baltimore, a town I love. Today I am speaking on PHP for WordPress, a topic near and dear to my heart since I learned PHP via WordPress and, as a teacher, guide others on this path as well.
After you check out the slide notes, please check out the Trailer for my PHP for WordPress Course from Treehouse.
The other week I had the great privilege of speaking at WordCamp Raleigh 2013. Here are my slides from the talk
I came across BackPress while reading about the WordPress Plugin API a while back. Basically it provides all of the core actions and filters from WordPress core to be used outside of the WordPress environment.
Not sure when I’ll have the chance to work with the framework, but it looks really cool and an awesome solution for the right problem.
Thanks to Jonathan Perez for this little function to use remove_action conditionally. Helped when I was working on a WooCommerce project.
EDIT: This file has been moved to woocommerce/includes/wc-template-hooks.php
Fully customizing WooCommerce did not make sense to me until I finally tracked down this file: /wp-content/plugins/woocommerce/woocommerce-hooks.php.
This file lists all of the hooks and all of the actions that are added to the hooks. So, if you’ve ever tried searching through the rather abstracted template files and not be able to find what you’re looking for to customize, check out the woocommerce-hooks.php file.
I didn’t find any direct mention of this file in the docs, so hopefully the 2.0 WooCommerce docs will have this file at least mentioned as the place to go to see the default loading of actions.
Have to say though, that once found, everything was super clear to figure out, so big thanks to the dev team for clean coding!