How to Create Your First Gutenberg Custom Block

Create your first custom block for the WordPress block editor

Since WordPress 5.0 the editor is built from blocks, and a plugin or theme can add its own. A block is registered in JavaScript with registerBlockType(): the edit function describes how it looks and works in the editor, the save function returns the HTML that is stored in the post. This post builds a simple “callout” box without any build tools.

Problem: Editors need a highlighted callout box, and a shortcode or a CSS class in custom HTML is too error-prone for them.

Solution: Write a small block in plain JavaScript (no JSX, so no build step), enqueue it on enqueue_block_editor_assets with the wp-blocks, wp-element and wp-editor dependencies, and add a stylesheet for the editor and the front end.

Loading the script

In a small plugin, for example wp-content/plugins/ha-callout/ha-callout.php:

<?php
/**
 * Plugin Name: HA Callout Block
 */

add_action( 'enqueue_block_editor_assets', function () {
    wp_enqueue_script(
        'ha-callout-block',
        plugins_url( 'block.js', __FILE__ ),
        array( 'wp-blocks', 'wp-element', 'wp-editor', 'wp-i18n' ),
        filemtime( plugin_dir_path( __FILE__ ) . 'block.js' )
    );
} );

add_action( 'enqueue_block_assets', function () {
    wp_enqueue_style(
        'ha-callout-style',
        plugins_url( 'style.css', __FILE__ ),
        array(),
        filemtime( plugin_dir_path( __FILE__ ) . 'style.css' )
    );
} );

The dependencies tell WordPress to load its own packages first; they are available as the global wp object. enqueue_block_assets loads the style both in the editor and on the front end.

The block

block.js, without JSX: wp.element.createElement does what <div> would do after compilation.

( function ( blocks, element, editor, i18n ) {
    var el       = element.createElement;
    var RichText = editor.RichText;
    var __       = i18n.__;

    blocks.registerBlockType( 'ha/callout', {
        title: __( 'Callout box' ),
        icon: 'megaphone',
        category: 'common',

        attributes: {
            content: {
                type: 'string',
                source: 'html',
                selector: 'p'
            }
        },

        edit: function ( props ) {
            return el( 'div', { className: 'ha-callout' },
                el( RichText, {
                    tagName: 'p',
                    value: props.attributes.content,
                    onChange: function ( value ) {
                        props.setAttributes( { content: value } );
                    },
                    placeholder: __( 'Write the callout text…' )
                } )
            );
        },

        save: function ( props ) {
            return el( 'div', { className: 'ha-callout' },
                el( RichText.Content, { tagName: 'p', value: props.attributes.content } )
            );
        }
    } );
} )( window.wp.blocks, window.wp.element, window.wp.editor, window.wp.i18n );

The attribute is read back from the saved HTML (source: 'html', selector: 'p'), so nothing extra is stored in the block comment.

The style

style.css:

.ha-callout {
    background: #fff3cd;
    border-left: 4px solid #ffc107;
    padding: 1rem 1.25rem;
}

.ha-callout p {
    margin: 0;
}

After activating the plugin, the block appears in the inserter under “Common blocks”.

Update (2019): WordPress 5.2 moved RichText and the other editor components from wp.editor to the new wp.blockEditor package (dependency wp-block-editor). The old names still work but log a deprecation warning; on 5.2+ use wp.blockEditor.RichText.

NOTE: If you change the save function after posts already use the block, the editor reports “This block contains unexpected or invalid content” for the old ones, because their stored HTML no longer matches. Plan the markup before using the block on real content, or add the old version to the block’s deprecated list.