Tag Archives: Templates

How to Add Block Templates to Your WordPress Theme or Plugin Gutenberg

How to Add Block Templates to Your WordPress Theme or Plugin

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 block template with and image and paragraph block

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.

Get Lesson Object for a Topic in LearnDash

While customizing LearnDash I came across an issue in the topic.php file where the $lesson_post variable was returning null even when there was a parent lesson for the topic.

So, after digging around at the LearnDash source code a bit I came up with this simple solution to grab the lesson whenever I needed using the topic post which is accessible via $post.

Not sure if others will find this helpful, but there are not a ton of extensive docs or articles on developing for LearnDash so wanted to share 🙂

How to Embed a Shortcode from a Plugin Directly into a Template File

I recently came across a great shortcode that I wanted to embed directly into a template, rather than use as a shortcode from within the admin area.

If you ever need to do this you can use the do_shortcode function, and pass any necessary parameters as you would need.

<?php echo do_shortcode('[shortcode with="parameters"]'); ?>

I’m Curious to See How Swift Templates Work in the Real World with Clients

Swift template example

Swift templates is a way to use a simple templating language to create custom templates from within the admin area, and even the Publish page.  I suppose for the right client and use case it would be helpful, but for the type of work I do creating simple sites for clients, it doesn’t really apply.

That said, I’m curious to see how it evolves and if anyone has experience using it.  You can read more about the feature here or check out the WordPress Creation Kit, which it is a part of.

Link

If you’re building an admin settings page for a plugin, you should check out this plugin to get easy, copy and paste code to create all the WordPress default layouts and markup.

Thanks Frank for making it so easy!