Skip to content

How to save a WordPress Gutenberg block as a template to use later with a shortcode?

When you're working on a WordPress site and you come across a Gutenberg block that you want to use multiple times, saving it as a template can save you a lot of time. In this article, we will discuss how to save a WordPress Gutenberg block as a template and how to use it later using shortcode.

Save a Gutenberg block as a template using a plugin

Reusable Gutenberg Blocks by Editor Blocks

One of the quickest and easiest ways to save a Gutenberg block as a template is to use a plugin such as Reusable Gutenberg Blocks by Editor Blocks. This plugin allows you to save any Gutenberg block as a template that you can easily use later. Here's how to do it:

  1. Install and activate the Reusable Gutenberg Blocks by Editor Blocks plugin from the WordPress plugin repository.
  2. Go to any page or post and add the block you want to save as a template.
  3. Select a block and then click the “Add to Reusable Blocks” button (at the top right of the editor).
  4. Give your block a name and save your changes.

You can later use this block on another page or post by selecting it from the list of available Gutenberg blocks.

Creating a shortcode using the Shortcoder plugin

To use the created Gutenberg block template using shortcode, we can use the Shortcoder plugin.

  1. Install and activate the Shortcoder plugin from the WordPress plugin repository.
  2. Go to the WordPress administration panel and then to “Shortcoder”.
  3. Click “Create a new shortcode”, give it a name and paste the HTML code of your Gutenberg block template.
  4. Save Changes.

Now, when you want to use this block, just paste the shortcode anywhere on your page or post.

Create a Gutenberg block template using PHP code

If you are familiar with coding, you may choose to take a more manual approach. Here's how to create a Gutenberg block template and shortcode using PHP code:

  1. Create a new PHP file in your theme or child theme. You can name it something like 'gutenberg-template.php'.
  2. In this file, place the HTML code for your Gutenberg block, but remember that you must place it inside a PHP function, like this:
function my_gutenberg_template() { ob_start(); ?> <?php
    return ob_get_clean();
}
  1. Then, to create a shortcode that will call this function, add the following code:
function register_my_gutenberg_template_shortcode() { add_shortcode('my_template', 'my_gutenberg_template'); } add_action('init', 'register_my_gutenberg_template_shortcode');

Now, when you want to use this block, just paste the '[my_template]' shortcode anywhere on your page or post.

To summarize, WordPress offers various methods to save a Gutenberg block as a template and then use it via shortcode. We can use available plugins, such as Reusable Gutenberg Blocks by Editor Blocks or Shortcoder, or create our own solution using PHP code. The choice of method depends on your experience and preferences.

[reflex id='56434']

Check out the offer of 1500+ Premium WordPress plugins and themes!