Search code examples
wordpresswordpress-gutenberggutenberg-blocks

How to remove panels from Gutenberg in cleared way?


I am looking for some of code which will help me delete panels from Gutenberg but specifically with the settings just for selected roles and not for every of them etc. I tried everything but problem is Gutenberg using panels and not metaboxes like Classic Editor before.

Is there someone, who knows how to turn off metaboxes in gutenberg for x user levels?

I tried https://codex.wordpress.org/Function_Reference/remove_meta_box , but as I said, that will no work because Gutenberg.

Thank you.


Solution

  • As you already know Gutenberg ignores remove_meta_box. You can remove a panel by doing the following:

    In PHP register a script. Also register an empty block and add a JS file to it.

    function hughie_register_files() {
        // script file
        wp_register_script(
            'hughie-admin-panels',
            plugins_url('/scripts/admin-panels.js', __FILE__),
            [ 'wp-blocks', 'wp-edit-post' ]
        );
        // register block editor script
        register_block_type( 'hughie/admin-panels', array(
            'editor_script' => 'hughie-admin-panels'
        ) );
    }
    

    In the JS file add any of the following lines:

    wp.data.dispatch('core/edit-post').removeEditorPanel( 'taxonomy-panel-category' ) ; // category
    wp.data.dispatch('core/edit-post').removeEditorPanel( 'taxonomy-panel-TAXONOMY-NAME' ) ; // custom taxonomy
    wp.data.dispatch('core/edit-post').removeEditorPanel( 'taxonomy-panel-post_tag' ); // tags
    wp.data.dispatch('core/edit-post').removeEditorPanel( 'featured-image' ); // featured image
    wp.data.dispatch('core/edit-post').removeEditorPanel( 'post-link' ); // permalink
    wp.data.dispatch('core/edit-post').removeEditorPanel( 'page-attributes' ); // page attributes
    wp.data.dispatch('core/edit-post').removeEditorPanel( 'post-excerpt' ); // Excerpt
    wp.data.dispatch('core/edit-post').removeEditorPanel( 'discussion-panel' ); // Discussion
    

    Your problem is you only want to include the file is the user has a certain capability.

    All WP hooks can be found at: https://codex.wordpress.org/Plugin_API/Action_Reference

    By the time init is called the user should be defined and available to check