Registering a sidebar

This is an example of how to add a sidebar inside the editor.
In this case it will display a sidebar with one tab, one panel and two settings inside it: buttons and checkbox.

The sidebar will appear in every:

  • post
  • my_cpt

These two settings are of 'data_type' => meta so their value will be saved to the post meta data.

Inside a php file in your plugin or the functions.php file of your theme call the filter in the following way:

add_filter( 'pmc_create_sidebar', 'myplugin_create_sidebar' );
function myplugin_create_sidebar( $sidebars ) {

    // Define the sidebar with its tabs, panels and settings.
    $sidebar = array(
        'id'              => 'mysidebar',
        'id_prefix'       => 'myidprefix_',
        'label'           => __( 'Sidebar label', 'my_plugin' ),
        'post_type'       => array( 'post', 'my_cpt' ),
        'data_key_prefix' => 'mydataprefix_',
        'icon_dashicon'   => 'carrot',
        'tabs'            => array(
            array(
                'label'  => __( 'Tab label', 'my_plugin' ),
                'panels' => array(
                    array(
                        'label'    => __( 'Panel label', 'my_plugin' ),
                        'settings' => array(
                            // Buttons setting.
                            array(
                                'type'          => 'buttons',
                                'data_type'     => 'meta',
                                'data_key'      => 'buttons_key',
                                'label'         => __( 'Setting label', 'my_plugin' ),
                                'help'          => __( 'Setting description', 'my_plugin' ),
                                'default_value' => 'bbb',
                                'options'       => array(
                                    array(
                                        'title'         => __( 'Option title aaa', 'my_plugin' ),
                                        'value'         => 'aaa',
                                        'icon_dashicon' => 'carrot',
                                    ),
                                    array(
                                        'title'         => __( 'Option title bbb', 'my_plugin' ),
                                        'value'         => 'bbb',
                                        'icon_dashicon' => 'sos',
                                    ),
                                ),
                            ),
                            // Checkbox setting.
                            array(
                                'type'          => 'checkbox',
                                'data_type'     => 'meta',
                                'data_key'      => 'checkbox_key',
                                'label'         => __( 'Setting label', 'my_plugin' ),
                                'help'          => __( 'Setting description', 'my_plugin' ),
                                'default_value' => false,
                                'use_toggle'    => true,
                                'input_label'   => __( 'Input label', 'my_plugin' ),
                            ),
                        ),
                    ),
                ),
            ),
        ),
    );

    // Push the $sidebar we just assigned to the variable
    // to the array of $sidebars that comes in the function argument.
    $sidebars[] = $sidebar;

    // Return the $sidebars array with our sidebar now included.
    return $sidebars;

}
PHP
Next Helpers