Add a Custom Sidebar Panel to the Gutenberg Editor with PluginSidebar

Add a custom sidebar to the block editor with PluginSidebar

The block editor has two tabs in its sidebar: Document (status, categories, tags) and Block (settings of the selected block). A plugin can add its own sidebar next to them, with a button in the top toolbar and an entry in the “More tools” menu. It is a good place for editorial data such as internal notes or a review status, instead of a classic meta box under the content.

Problem: Editors need an extra field for internal notes in the block editor, without a classic meta box below the content.

Solution: Register the meta field with register_post_meta() and show_in_rest, so the editor can read and save it. Then register a plugin with registerPlugin() that renders a PluginSidebar with a TextareaControl connected to the meta field.

The PHP side

<?php
add_action( 'init', function () {
    register_post_meta( 'post', '_ha_editor_notes', array(
        'show_in_rest'  => true,
        'single'        => true,
        'type'          => 'string',
        // meta keys starting with _ are protected; allow editing for users who can edit posts
        'auth_callback' => function () {
            return current_user_can( 'edit_posts' );
        },
    ) );
} );

add_action( 'enqueue_block_editor_assets', function () {
    wp_enqueue_script(
        'ha-editor-notes',
        get_template_directory_uri() . '/js/editor-notes.js',
        array( 'wp-plugins', 'wp-edit-post', 'wp-element', 'wp-components', 'wp-data', 'wp-compose' ),
        filemtime( get_template_directory() . '/js/editor-notes.js' ),
        true
    );
} );

Without show_in_rest the editor cannot see the field: the block editor loads and saves the post through the REST API. The post type must also support custom-fields, which normal posts do.

The sidebar

js/editor-notes.js, plain JavaScript without a build step:

( function ( wp ) {
    var el              = wp.element.createElement;
    var Fragment        = wp.element.Fragment;
    var PluginSidebar   = wp.editPost.PluginSidebar;
    var MoreMenuItem    = wp.editPost.PluginSidebarMoreMenuItem;
    var TextareaControl = wp.components.TextareaControl;
    var withSelect      = wp.data.withSelect;
    var withDispatch    = wp.data.withDispatch;
    var compose         = wp.compose.compose;

    var NotesField = compose(
        withSelect( function ( select ) {
            return { notes: select( 'core/editor' ).getEditedPostAttribute( 'meta' )._ha_editor_notes };
        } ),
        withDispatch( function ( dispatch ) {
            return {
                setNotes: function ( value ) {
                    dispatch( 'core/editor' ).editPost( { meta: { _ha_editor_notes: value } } );
                }
            };
        } )
    )( function ( props ) {
        return el( 'div', { style: { padding: '16px' } },
            el( TextareaControl, {
                label: 'Notes for the editor',
                value: props.notes || '',
                onChange: props.setNotes,
                rows: 8
            } )
        );
    } );

    wp.plugins.registerPlugin( 'ha-editor-notes', {
        icon: 'edit',
        render: function () {
            return el( Fragment, null,
                el( MoreMenuItem, { target: 'ha-editor-notes' }, 'Editor notes' ),
                el( PluginSidebar, { name: 'ha-editor-notes', title: 'Editor notes' }, el( NotesField ) )
            );
        }
    } );
} )( window.wp );

withSelect reads the current (unsaved) value from the editor store, withDispatch changes it; the value is saved together with the post when the user clicks Update.

If you prefer a small panel inside the Document tab instead of a whole sidebar, use PluginDocumentSettingPanel (WordPress 5.3+) with the same field. A simple custom block without a build step is shown in creating your first custom block.

NOTE: WordPress 5.3 also added the React hooks wp.data.useSelect and useDispatch, which do the same as the higher-order components above with less wrapping, also without JSX. Use them when the site runs 5.3 or newer; the version above also works on 5.0–5.2.