WordPress Gutenberg Tutorial for Developers

The Great WordPress Gutenberg Tutorial for Developers - How to Create Your Own Blocks

Gutenberg opens up exciting new possibilities for you as a developer. In this tutorial I explain everything you need to know about the block editor. Learn how to create your own WordPress blocks and how to customize existing blocks.

If you haven’t been following the latest in WordPress, Gutenberg is the editor released with WordPress 5.0. It introduced the concept of WordPress blocks, which empowers website owners to drag and drop design elements without the need for an external page builder or coding knowledge. 

Gutenberg is eventually going to be used as a foundation for full site editing. In the past, coding was always necessary, even if you used a page builder, as some tweaks and adjustments must be done with code.

However, as each new update rolls out, it’s become clear that knowing PHP and traditional WordPress website development alone is no longer enough. You must master Gutenberg if you’re a WordPress developer or even just want to follow a WordPress tutorial for developers.

This guide will cover: 

  • What Gutenberg is
  • How to work with WordPress blocks
  • How to make use of these new features and technologies to build powerful WordPress websites

Let's dive into the WordPress Gutenberg tutorial for developers.

What is Gutenberg?

Gutenberg first rolled out in WordPress 5.0 to make it easy to create posts and pages in a purely visual way. But this was only Phase 1 of the project.

Gutenberg relies on modern technologies such as JavaScript, React and REST APIto create a dynamic editor experience that imitates front-end editing. This initial phase saw the release of the block editor

What is the Gutenberg Block Editor?

Gutenberg replaces the TinyMCE editor (often referred to as the classic editor) and lets you use content blocks to add elements such as text or images to your posts and pages.

As more updates roll out and Phase 2 comes to fruition, Gutenberg aims to become a full-fledged website editing tool. Eventually, you could build an entire WordPress website using blocks.

WordPress Gutenberg Tutorial for Developers

This means those who developsWordPress websites need to make adjustments to how they do things. Knowing PHP is no longer enough to build a WordPress website from scratch. You’ll need to know JavaScript, React, and REST API if you want to create custom blocks and design elements for Gutenberg-powered websites. 

Intro to WordPress blocks

It’s exciting to embark on this new journey with the Gutenberg block editor. Consider this your primer on what you need to know before you get started or create WordPress custom blocks of your own.

What You Need to Know Before Developing for Gutenberg 

The primary language you’ll need to know to follow this WordPress Gutenberg tutorial for developers is Javascript. Specifically, you need to know version ES5, which is typically supported by most modern browsers. 

Newer features are included in the 2015 release, ES6, and the latest version, ESNext, which is a loose set of proposals without official support. 

Understanding ES6 and ESNext can be valuable. However, if you want to use them in your development of WordPress blocks, you need to know how to configure tools that transform your newer code into ES5. 

WordPress Gutenberg Tutorial for Developers

You also need to understand JSX, which is a “syntax-extension” of Javascript and also requires transformation. Since ES6, ESNext, and JSX are so much simpler to write than ES5, it’s worth the effort to set up transformation tools. 

WordPress Gutenberg Tutorial for Developers

Webpack, in conjunction with Babel, is the typical transformation tool choice. This tool combination minifies your Javascript. 

WordPress Gutenberg Tutorial for Developers

Lastly, knowledge of React JS can help you immensely when you create your own WordPress blocks, or follow various WordPress tutorials for developers that focus on block customization. 

Content Is Stored Differently in Gutenberg 

Unlike the classic content editor, content is stored differently in Gutenberg. Content is no longer stored as HTML, but rather as HTML comments in a certain structure with JSON format. This makes it easier to transfer content into another WordPress installation or content management system (CMS). 

Storing content in traditional HTMLis fine when it’s simple and text-based. However, if you’re dealing with any dynamically-generated content, the code begins to get sloppy. 

You don’t necessarily need to know the technical details about how WordPress blocks work. However, for this WordPress Gutenberg blocks tutorial, when you create content in Gutenberg, the HTML produced is free of parsed and messy code because all the details are stored within HTML comments. 

What You Can Do With the WordPress Gutenberg Blocks 

You can do several things in Gutenberg. When you follow a WordPress tutorial for developers, you need to be aware of your options. 

Let’s explore a few: 

  • Develop custom WordPress blocks: This is the most complex task you can complete, but it’s often worth the effort to achieve true customization. 
  • Prepare your theme(s) for Gutenberg : This involves following the suggested theme supports  provided by WordPress.org to keep your theme Gutenberg-compatible. It also involves adjusting block styling to maintain the look you want for your theme while retaining block functionality. 
  • Customize or extend existing WordPress content blocks: As mentioned above, you can customize existing WordPress blocks in Gutenberg . This involves either adding custom block styles, adding filters via JavaScript, or adjusting block categories and block types to appear for specific custom post types. 

Set Up a Development Environment for the Gutenberg Block Editor

WordPress Gutenberg Tutorial for Developers

Before you can create anything with Gutenberg or complete any WordPress tutorials for developers, you need to set up a development environment. Your first step is to get Node.js. WordPress recommends using the Node Version Manager (nvm) to install and manage Node. 

Within the cloned repository, you can build Gutenberg by running the following: 

npm ci
npm run build

This coding makes it so Gutenberg can be used as a Plugin can be used. Then, add the Gutenberg directory you created in the wp-content/plugins folder in your local WordPress environment. A White Pixel also recommends installing Webpack and Babel. 

Go through all the setup steps to configure a development environment. You need a local test website running WordPress with the Gutenberg plugin you built and access to Terminal to complete a variety of command-line prompts.
WordPress.org offers numerous resources for  development scripts

With your local environment set up and configured, we can now move on to the main part of this WordPress tutorial for developers: WordPress blocks.

Understanding WordPress blocks

WordPress blocks are containers in which content is placed within the WordPress block editor and are capable of being moved around via a drag-and-drop interface.

Understanding what WordPress blocks are is essential before you create your own. WordPress blocks are also wide-ranging and encompass  several key features, including those related to: 

How to Create Custom WordPress Blocks: A WordPress Tutorial for Developers

With this WordPress Gutenberg tutorial for developers, you will learn what you need to create custom WordPress content blocks. Then, you’ll be able to be use them on your or your clients’ websites. 

How to Register New WordPress Blocks

Every new block begins with registration. Use the registerBlockType function to register a new block type definition.  

Block name is a string that has to be written as namespace/block-name. Swap out namespace for the name of what you’re creating. 

From there, you need to define the properties of your new block, like its title, description, category, icon, keywords, or styles. 

How to Add Toolbars for Custom Actions

If you want to add a custom set of actions to your block via an interactive toolbar, you can customize (or create from scratch) workable Gutenberg toolbars. Set custom icons, formatting, positioning, and assigned actions depending on your plugin or theme’s needs. 

WordPress Gutenberg Tutorial for Developers

According to the Block Editor Handbook, adding a custom toolbar might look something like this: 

Import Toolbar

How to Translate WordPress Blocks 

Completing internationalization steps can help ensure any WordPress blocks you create will reach a wider audience. 

Since WordPress 5.0, you can use the wp-i18n JavaScript package to add translatable strings. You can also create translation files and have them served when a user requests content in that language. 

How to Add Inspector Settings

All blocks have a set of inspector settings that appear on the right-hand side of the screen when inserted into your content. And depending on the WordPress block, the aspects users can customize will be different. Any custom block you create needs to have its inspector settings defined. 

WordPress Gutenberg Tutorial for Developers

Du kannst <InspectorControls> hinzufügen, indem du WordPress mitteilst, welche Inspektor Steuerelemente verwendet werden sollen, wie sie aussehen sollen, welche Optionen enthalten sein sollen und in welcher Reihenfolge sie erscheinen sollen. 

JavaScript for WordPress’s efforts look something like this: 

WordPress Gutenberg Tutorial for Developers

With these specifics defined, you can then use JSX to style each function and adjust their order. 

How to Output Dynamic Content Using PHP

Dynamic content is essential for WordPress. There are many reasons you might need a dynamic block. 

For example, if you want to create a custom “latest posts” block that displays the latest blog posts to be published in real-time, you’d need a dynamic block. 

The block relies on PHP because it needs to retrieve new information each time it’s rendered. The key to making a block dynamic is to change its save function to null. Then insert code to instruct the new block to retrieve the appropriate content. 

You can also set an attribute to instruct the block to display a certain number of posts.

How to Add Custom Settings to Preexisting Gutenberg Blocks

You can also add custom settings and make tweaks to existing Gutenberg WordPress blocks. For instance, you could add custom controls to the advanced block panel within a block’s inspector settings. Jeffrey Carandang has written a great tutorial about this on his blog. To do this, add a custom attribute to the core Gutenberg blocks. Then, add the control for this attribute to the advanced section of the block’s inspector settings. And finally, add the custom element class to the individual block itself.

About Block Patterns and How to Create Custom Block Patterns

Let’s explore another aspect of Gutenberg in this WordPress tutorial for developers. With WordPress block patterns, you can build and share predefined block layouts. These are a grouping of WordPress blocks that empower you to create complex layouts by clicking a few buttons.

Block patterns can be accessed in the post/page editing screen by pressing the + sign. You’ll then see all the available block patterns for your website. 

WordPress Gutenberg Tutorial for Developers

How to Create a Custom Block Pattern 

To begin, build a draft page or post. Insert blocks in any configuration you’d like. For instance, if you plan on running an online magazine, you might want to create a page layout for new articles with prominent titles, columns, images, and quotes. 

Next, style the blocks so they look how you want. 

The next step is to copy everything you’ve made so far. Click all your blocks while holding down the ALT key to select them. Then click the three stacked dots icon (more options) on the toolbar. Click Copy. 

WordPress Gutenberg Tutorial for Developers

Next, you need to escape the HTML output you’ve just copied. To do this, you can use the JSON Escape/Unescape tool. Copy the Result String code that appears after clicking Escape

WordPress Gutenberg Tutorial for Developers

Now you need to register your new block pattern. 

How to Register and Unregister Block Patterns

You can register a custom block pattern using the Block Pattern API to add the code you copied above to your WordPress theme’s functions.php file or by adding it to a custom plugin.

You can also use the Block Pattern Builder plugin to eliminate the coding requirements for creating block patterns.  

WordPress Gutenberg Tutorial for Developers

WordPress Tutorial for Developers: How to Use Gutenberg Editor as a Theme Developer

Gutenberg makes it easy to develop custom WordPress blocks that make content publishing a rich experience. But you can also use Gutenberg to eliminate the need for a separate page builder plugin like Elementor or Divi.

Gutenberg can be used to create engaging front-page layouts by enabling full-width and wide blocks. Or, you can create a block-based theme from scratch. 

Let’s discuss a few of your options in greater detail. 

Using Cover Blocks for Adding Page Sections

Cover blocks are a newer addition to Gutenberg and are a great way to add different sections to a page. For more information, check out this video.

What are cover blocks?

Cover blocks are image blocks which you can overlay text on. They’re a great way to create header sections or custom-designed sections you’d expect to find in page builder WordPress plugins or within WordPress themes.

Cover blocks you can add include:

  • Image blocks
  • Video backgrounds
  • Heading blocks
  • Paragraph blocks
  • Button blocks 

Cover blocks add versatility to your site-building efforts. In effect, you can create a custom homepage this way. You could even build a simple WordPress theme using a combination of cover blocks and block patterns. 

Even more expansive full site editing capabilities are on the horizon as well. The Make WordPress Design team indicates template manipulation, advanced block patterns, and global styles as the primary focus areas. 

Use of the Advanced Custom Fields plugin 

One limitation of blocks is that you can’t achieve a full-width layout. It will always be boxed. 

However, with some clever manipulation, you can use the Advanced Custom Fields plugin to add custom meta settings and force full-width layouts. This is essential if you want to use Gutenberg to create custom themes. 

WordPress Gutenberg Tutorial for Developers

Use Custom Block Styles 

Another way to use Gutenberg as a theme developer is to use custom block styles. Once you’ve created block patterns or used cover blocks to create page sections, you can then add or remove block styles from each block involved. 

You’ll need to know the block’s name so you can then attach or remove styles associated with it. This extra step makes it so the custom CSS class won’t have to be inserted each time the block is used. 

WordPress Blocks for Landing Pages and Front Pages

We already mentioned how effective cover blocks can be for creating homepages for your website or theme. They work great for landing pages as well. 

However, a few other blocks worth mentioning that can aid in this task include: 

  • Galleries
  • Column blocks
  • Image blocks
  • Table blocks
  • Text blocks

Most blocks can be used in conjunction with the cover image blocks to create a professional-looking home page. 

If you need more features, you can add a Gutenberg block plugin, which adds a single block to Gutenberg, or a Gutenberg block library plugin, which adds several. 

Ultimate Addons for Gutenberg and Atomic Blocks are standout library options that offer an easier site-building experience with blocks for testimonials, a call to action, page layout options, a contact form, a pricing table, and shortcodes. 

WordPress Gutenberg Tutorial for Developers

Plugins are those who want to get acquainted with Gutenberg and develop websites without having to use much code. 

Final Thoughts: WordPress Blocks: A Big WordPress Tutorial for Developers

The WordPress Gutenberg block editor is now a part of core WordPress, and the development team behind WordPress has big plans for it. Eventually, you’ll be able to use Gutenberg to create navigation menus, and it will be compatible with even more themes.

Now, you can use WordPress blocks to build websites and tailor content to your needs with the help of this WordPress tutorial for developers. Hopefully, you’ve found this Gutenberg tutorial helpful. You can now create WordPress blocks from scratch, and eventually, create entire WordPress websites using the built-in block editor. 

You can now create WordPress blocks from scratch and eventually build entire WordPress websites using the built-in block editor.
In the meantime, you should make sure that the websites you develop arehosted at Raidboxes\. Raidboxes offers comprehensive WordPress hosting packages suitable for both individual website owners and agencies.

Your questions about Gutenberg Development

You have questions for Maddy about the Gutenberg Guide for developers? We are looking forward to your comment. You want to be informed about new postsaround WordPress? Then follow Raidboxes on Twitter, Facebook or via our newsletter.

Did you like the article?

Your rating helps us improve our future content.

Post a comment

Your email address will not be published. Required fields are marked with *.