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:

// Get registerBlockType() from wp.blocks in the global scope
const { registerBlockType } = window.wp.blocks;

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.

// Get registerBlockType() from wp.blocks in the global scope
const { registerBlockType } = window.wp.blocks;
// Two parameters, name and settings
registerBlockType( 'example-plugin/example-custom-block', {} );

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 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.

// Get helper functions from global scope
const { registerBlockType } = window.wp.blocks;
const { __ } = window.wp.i18n;
// Use registerBlockType to create a custom block
// Localize title using wp.i18n.__()
title: __( 'Block Title' ),
// Category Options: common, formatting, layout, widgets, embed
category: 'common',
// Dashicons Options –
icon: 'wordpress-alt',
// Limit to 3 Keywords / Phrases
keywords: [
__( 'Example' ),
__( 'Project' ),
__( 'Code Samples' )
// Attributes set for each piece of dynamic data used in your block
attributes: {
exampleContent: {
type: 'array',
source: 'children',
selector: '',
// Determines what is displayed in the editor
edit: props => {
const onChangeContent = value => {
props.setAttributes( { exampleContent: value } );
return (
<div className={props.className}>
placeholder={ __( 'Add your content…' ) }
// Determines what is displayed on the frontend
save: props => {
return (
<div className={props.className}>

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.

2 thoughts on “How to Use to registerBlockType() to Create Blocks in WordPress

  1. Great article. I have a question when you have
    attributes: {
    exampleContent: {…},
    }, you get it like const {exampleContent} = attributes; in es6 but how do you get query attributes like
    images: {
    source: ‘query’
    selector: ‘img’,
    query: {
    url: { source: ‘attribute’, attribute: ‘src’ },
    alt: { source: ‘attribute’, attribute: ‘alt’ },
    Like this : const {url,alt} = attributes.images; ?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s