Add a Custom Panel to the Gutenberg Document Sidebar with PluginDocumentSettingPanel

Add a custom panel to the Document sidebar with PluginDocumentSettingPanel

The Document tab of the block editor holds the post settings: status, permalink, categories, featured image. Since WordPress 5.3, plugins can add their own collapsible panel there with PluginDocumentSettingPanel. For a field that belongs to the whole post, like a release year or a “featured” flag, this is a better place than a separate sidebar: editors find it next to the other post settings.

Problem: A movie post type has a release year in post meta, and editors should set it in the Document tab rather than in a meta box under the content.

Solution: Register the meta with register_post_meta() and show_in_rest, then register a plugin that renders a PluginDocumentSettingPanel with a field connected to the meta through useSelect and useDispatch (WordPress 5.3+).

The meta field and the script

<?php
add_action( 'init', function () {
    register_post_meta( 'movie', 'ha_release_year', array(
        'show_in_rest'  => true,
        'single'        => true,
        'type'          => 'integer',
        'auth_callback' => function () {
            return current_user_can( 'edit_posts' );
        },
    ) );
} );

add_action( 'enqueue_block_editor_assets', function () {
    if ( 'movie' !== get_current_screen()->post_type ) {
        return;
    }

    wp_enqueue_script(
        'ha-movie-panel',
        plugin_dir_url( __FILE__ ) . 'js/movie-panel.js',
        array( 'wp-plugins', 'wp-edit-post', 'wp-element', 'wp-components', 'wp-data' ),
        filemtime( plugin_dir_path( __FILE__ ) . 'js/movie-panel.js' ),
        true
    );
} );

The post type must support custom-fields ('supports' => array( 'title', 'editor', 'custom-fields' )), otherwise the REST API does not return the meta and the panel stays empty.

The panel

js/movie-panel.js, without a build step:

( function ( wp ) {
    var el            = wp.element.createElement;
    var useSelect     = wp.data.useSelect;
    var useDispatch   = wp.data.useDispatch;
    var TextControl   = wp.components.TextControl;
    var DocumentPanel = wp.editPost.PluginDocumentSettingPanel;

    function MoviePanel() {
        var year = useSelect( function ( select ) {
            var meta = select( 'core/editor' ).getEditedPostAttribute( 'meta' ) || {};
            return meta.ha_release_year;
        }, [] );

        var editPost = useDispatch( 'core/editor' ).editPost;

        return el( DocumentPanel, { name: 'ha-movie-details', title: 'Movie details' },
            el( TextControl, {
                label: 'Release year',
                type: 'number',
                value: year || '',
                onChange: function ( value ) {
                    editPost( { meta: { ha_release_year: parseInt( value, 10 ) || 0 } } );
                }
            } )
        );
    }

    wp.plugins.registerPlugin( 'ha-movie-panel', { render: MoviePanel } );
} )( window.wp );

useSelect reads the edited (not yet saved) value, editPost changes it in the editor; the value is saved together with the post. The name of the panel must be unique; it is also used to remember whether the panel is open or closed.

Panel or sidebar

A panel in the Document tab suits a few post-level fields. For a bigger tool (notes, a checklist, an SEO analysis), a separate sidebar with its own toolbar button, built with PluginSidebar, gives more room; it is shown in adding a custom sidebar to the block editor.

NOTE: Validate the value on the server as well. The panel sends whatever the editor typed, and the REST API checks only the registered type. Add a sanitize_callback to register_post_meta(), for example one that keeps the year between 1888 and next year.