Unlimited Wordpress themes, plugins, graphics & courses! Unlimited asset downloads! From $16.50/m
Advertisement
  1. Web Design
  2. WordPress Gutenberg
Webdesign

How to Create Custom Blocks for Gutenberg

by
Difficulty:IntermediateLength:ShortLanguages:

Gutenberg has changed the way users create content within WordPress, and the way developers build WordPress plugins and themes. In this tutorial you’re going to learn how you can create custom blocks for Gutenberg.

Video: Custom Blocks for Gutenberg

A Quick Introduction to Gutenberg

Gutenberg is a totally new way for people to create content for their WordPress sites. The classic editor hasn’t changed much since WordPress was created back in 2003, and while it has always been fine for creating text-heavy blog posts, it’s not ideal for today’s media-rich websites with more complex layouts. Gutenberg is WordPress’ own response to the page builder plugins used by so many websites nowadays.

At Tuts+ we have a learning guide to introduce you to Gutenberg: An Introduction to WordPress Gutenberg, plus this simple quick start video:

In addition to these resources we have a number of courses to get you started.

Lastly, take a look at these Gutenberg-specific themes if you’re interested in seeing what other developers are doing with it:

Methods for Creating a Custom Block

When it comes to creating custom blocks for Gutenberg you basically have two options:

  • Code it from scratch
  • Use a plugin

The first of these is logically the most difficult; it requires more coding knowledge as you have to register your blocks and customise them via JavaScript.

In this tutorial we’re going to take the easier route and use a plugin. The advantages of using a plugin are that we can build blocks using a visual interface, and that we don’t need to worry about registering the blocks with JavaScript. In fact, we won’t need to write any JavaScript code at all.

Plugins for Building Gutenberg Blocks

Let’s look at the plugin options we have available to us. 

Block Lab

This is actually the one we’re going to use. It’s free, and perfect for our needs.

Lazy Blocks

Whilst I haven’t actually worked with this plugin yet, its reviews and ratings are very solid and suggest it’s a good option too.

Creating a Simple Block

Begin by downloading and installing the Block Lab plugin. You can find it from within your WP admin, under Add New Plugin. A new set of menu items will be added to your WP admin:

As we’re using the free version of the plugin we need only a couple of these new menu items. Click on Add New.

What Are We Creating?

We’re going to build a small CTA (Call to Action) block. It will have a title, a description, a button, and some helper text. We’ll also need some fields to allow the user to change the color of our CTA, to add a border if they want, and also add a background. Lastly we’ll create a toggle to allow users to choose whether or not to display the helper text.

So, we give our block a title (“CTA”) which will immediately assign it an ID (slug) which we’ll need later. We then state what kind of block it will be. We’ll keep ours as a common block.

We can then add keywords. These are used to find blocks when the user is creating a page layout. Hit Publish and we’ll move onto the next step where we add block templates.

There are two parts to adding a block template. There’s what’s displayed in the backend editor, and what’s shown on the frontend. For the frontend aspect of this we’ll need to write some HTML code which will output our block contents the way we want. We’ll also need to slot some PHP functions into the markup to pull the actual content in.

Begin by Adding a Blocks Folder

In the root of your currently active theme you’ll need to add a folder called “blocks”. We’ll add our new template files to this folder. And if you forget where these files are supposed to go, you can see the path referenced in the screenshot above. Our first file will be called “block-cta.php”.

With that done we can now start adding fields which the user will use to populate the block. Here you’ll see I’m adding Title, which generates a handle of the same name. We’ll need the handle in order to output the contents into our template.

This part can take a while–so begin adding all the fields you need, giving them names, types, default values, whether they’ll be present in the Editor or the Inspector (the sidebar),  and whatever else you think they need. Watch the video above to see me walk through this process.

Here are all the fields I end up with:

With that saved, we can already go to a page and add our CTA block:

Here’s what our block editor looks like, with all the fields we added:

We can fill out these fields already, though nothing will be displayed in the backend because we haven’t yet built a template.

Rendering to the Backend and Frontend

Here’s the chunk of code I put together to hold the block content and output the values entered in the fields:

It’s quite self-explanatory. There’s a container div, then an <h3> heading to hold the title. The description is outputted, as is the button. Then there’s a condition to determine whether or not to display the helper text. Each bit of data is output using the block_field() function.

We can add styling too, by creating a subfolder in our blocks folder. Call it “css”, then add another file: block-cta.css and place your styles within it. 

These are the styles I used for this demo:

The block, when rendered to the frontend, looks like this now:

The same styles are applied to the backend editor too:

Conclusion

This tutorial has given you a good idea of what’s needed to create your own Gutenberg blocks. Hopefully you can see a world of potential opening up in front of you! Gutenberg blocks are a really useful way of designing and reusing chunks of content–I look forward to seeing what you create with them!

Related links:

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.