Tag Archives: Gutenberg

WordCamp Miami Gutenberg Workshop 2018

This year, WordCamp Miami continued it’s focus on providing resources around learning JavaScript for WordPress.  Not only did they have a dedicated JavaScript track like they have in the past, they also had a full day workshop on Gutenberg Development.

Brian Richards, Grzegorz (Greg) Ziółkowski, Josh Pollock and myself each did part of the workshop that broke down as follows:

  1. Future of WordPress Development – Grzegorz Ziółkowski
  2. Setup & Introduction to Gutenberg: Tooling And Terminology – Zac Gordon
  3. Creating Your Very First Gutenberg Block – Brian Richards
  4. Building Custom Gutenberg Blocks: From Static to Dynamic – Josh Pollock

You can still go back and watch the entire live stream here:

You can also find my slides to my portion of the workshop here:

 

You can also find the GitHub repo of example files for the workshop here.

Definitely Recommend the Next WordCamp Miami!!!

If you have a chance to make it to the next WordCamp Miami I highly suggest it!  It is always a great camp and they have a tendency to put a strong focus on teaching new and emerging technologies, like JavaScript and Gutenberg.

How to Use wp.i18n.__() to Allow for Translation of Your Gutenberg Block Text

While working on my Gutenberg Development Course I was really happy to find out that the core developers ported over much of the internationalization features we are familiar with on the PHP side into a client side library available as wp.i18n.

Example of __() in Action

 

Since wp.i18n is available in the global scope, you can access it directly without needing to officially “import” anything.  In this example we are pulling out the __ function from the wp.i18n library it exists within.

Notice when we call the function, we do not need to pass in a second parameter with the text domain for our plugin or theme.  This will be automatically sorted out for you.

Additional Internationalization Functions
We also have access to some other helpful internationalization functions that you may recognize from the PHP side.

  • wp.i18n.__()
  • wp.i18n._x()
  • wp.i18n._n()
  • wp.i18n._nx()
  • wp.i18n.sprintf()

Most of the time you only need __(), but if you are familiar with these other functions already in PHP or just want to dig deeper, you can explore the core library readme file here.

Learn More About Gutenberg Development

If you would like to learn more about building with the new editor in WordPress, please check out my Gutenberg Development Course 🙂

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.

Custom Tooltip Button in Gutenberg WordPress with JavaScript

How to Use the Tooltip Component in Gutenberg

While working on my Gutenberg Development Course I realized that default toolbar buttons have a nice Tooltip that appears over them on hover.

Gutenberg Link Tooltip.png

Example of the tooltip component in action with the Bold button

I also remembered seeing a Tooltip component in Gutenberg when walking through the source code.  It turns out it is pretty easy to add these in our own block as well.

Setting Up the Tooltip Component

In order for the Tooltip component to work you simply have to get it from the wp.components library.  It is found at wp.componentns

Then you simply wrap <Tooltip></Tooltip> around whatever you want to trigger the tooltip.

The Tooltip takes two parameters:

  • text [required] = What text should appear in the tooltip
  • position [default “top”] = Determines where the tooltip will appear in relation to the child content

Here is an example of Tooltip in action:

NOTE: This example also includes the Button and Dashicon components.  This is not necessary.  Tooltip can be wrapped around other components, but I have only been able to get it working in the editor, not on the frontend.

Here is a look at what the code above can create:

High Contast View.png

Example of tooltip applied to custom block button

Want to Learn More About Gutenberg?

If you would like to learn more about working with JavaScript in WordPress, check out my Gutenberg Development Course.

Sneak Peak Video from My Gutenberg Development Course – “An Overview of registerBlockType”

At WordCamp US in early Dec 2017 I set out a goal to release a Gutenberg Development Course by the end of the year.  As of writing it is New Years Eve 2017 and I just made it!

One of the important functions I learned about while working on the course is registerBlockType, which is used to register to setup blocks.  This video gives an overview of the function.

An Introduction to registerBlockType

In this video we discuss the following:

  • What the registerBlockType function does
  • What major parameters and setting you need when looking at it
  • A few simplified code examples
  • Sneak peak of the blocks we will use to learn more about registerBlockType

CORRECTION: At 05:10 in the slide should look like below:

fixed-screenshot.png

Corrected Slide at 05:10 in with function and JSX braces

To Learn More About Gutenberg Development

If you would like to learn more about developing with the new editor in WordPress, please check out my Gutenberg Development Course.

How to set a custom block color scheme in your theme for Gutenberg

How to Add a Custom “Color Palette” to Your WordPress Theme

One of the cool things I picked up while working on my Gutenberg Development Course was how to set a default color palette for all blocks inside your theme.

Interestingly, as far as I can tell, defining custom color schemes is something only a theme should do and this cannot be set from within a custom block.

Block Color Palettes in Gutenberg

One of the features that blocks can have in WordPress is the ability change a color. For example, the core paragraph block has the ability to change the background and text color in the block inspector panel.

Screen Shot 2017-12-30 at 12.59.31 PM.png

Example of Default Block Color Scheme in WordPress

One of the cool things you can do as a theme developer, is set your own custom color palette for your theme.  This can help users choose colors that match the design of the theme.

Setting Custom Color Palettes in Gutenberg

To set a custom color palette (or color scheme) you would simply add add_theme_support( ‘editor-color-palette’ ) to your functions.php file and then continue to pass in the hexadecimal values for colors you want.

You can add as many colors as you like (I think), but I would suggest limiting the selection to colors that work well with your theme.

You will then want to hook that add_theme_support() into ‘after_setup_theme’.

What This Looks Like in Action

Using the color combinations set above, the default color options for blocks would now look like this.

Example of custom color scheme applied to WordPress blocks

Example of custom color scheme applied to WordPress blocks

I think that if you have a theme with a specific color palette that you should definitely take the extra few minutes to set a default color scheme for the blocks.

Learn More About Developing with Gutenberg

To learn more about building custom blocks or customizing Gutenberg with your theme, please check out my course on Gutenberg Development Course.

How to Use to registerBlockType() to Create Blocks in WordPress

I have wanted to write a blog post on the registerBlockType() JavaScript function in WordPress for a while now.  While working on my Gutenberg Development Course I learned that this function is at the heart of Block Development in WordPress since you must use it to create custom blocks.

How to Access registerBlockType()

The creation of a block in WordPress (as of writing) is done via JavaScript (not PHP) using a function called registerBlockType().

This function lives inside of the wp.blocks library, which contains helpful functions for block creation, as well as the core WordPress blocks themselves.

WordPress makes this function accessible as a global variable in the window object as wp.blocks.registerBlockType().

Here is a shortcut you can add at the top of your JS file to “import” this function into your code:

NOTE: At the time of writing, wp.blocks is NOT available as a package you can actually import into your code as you would other packages.  It is only available via the global window object.

registerBlockType Name and Settings Parameters

The registerBlockType() function takes two parameters: name and settings.

  • name [string] –  “Block names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-block” Source
  • settings [Object] – The settings parameter is an object with several predefined properties that must be assigned for your block to work.  We will look at these in more depth.

Here is what this would look like without all of the settings configured.

registerBlockType Settings

The registerBlockType() settings object has seven properties that include both meta information about the block as well as methods controlling the UI and functionality for the block in the editor and on the frontend.

  • title [string] – The title setting gives your block a searchable, human readable name. It should be escaped using wp.i18n.__().
  • category [string] – The category setting determines under which heading a user can find your block. Options include “common”, “formatting”, “layout”, “widgets” and “embed.”
  • icon [Dashicon|Element] – The icon setting for registerBlockType determines what icon will represent your custom block. Can use a WP Dashicon or custom SVG.
  • keywords [Array] – The keyword setting provides three additional keyword / phrases that will display your block when searched for. Limited to 3.
  • attributes [Object] – The attribute setting identifies what dynamic content we are using in our blocks. Several attribute options exist depending on what types of data we are using. Attribute settings are optional if your block uses no dynamic data. This data is then made available via props.attributes.name. Read more.
  • edit [function] – The edit setting determines what be displayed in the editor area for the block. Must return a valid React element using wp.element.createElement() or JSX. Also accepts dynamic data as a props parameter.
  • save [function] – The save setting determines what be displayed when the block is converted to content for the frontend. Must return a valid React element using wp.element.createElement() or JSX. Also accepts dynamic data as a props parameter.

The first four of these [title, category, icon and keywords] are more for the user and include meta information about the block itself.  The last few settings [attributes, edit, save] are a bit more complex and control the actual functionality of a block.

Example of registerBlockType in Action

In order to show all of the settings in action, particularly the attributes setting, we need to have a block with some sort of dynamic data.

Below is an example of a simple block with one editable field.  This shows registerBlockType() in action with all it’s parameters and settings.

Depending on your comfort level with JavaScript (and possibly React) the code above may make sense, or there may be a bit inside of attributes, edit and save that doesn’t make complete sense.

Hopefully though, this post helps you understand the basics of how registerBlockType() works and gives you a basis to get started with it.

Learn More About Building Blocks in WordPress

To learn more about building blocks and other aspects of Gutenberg Development, please check out my Gutenberg Development Course.