There has been a while since the WP block editor, or “Gutenberg”, was first introduced to the community. While the possibilities of the editor are obviously immense, especially if compared to the classic editor, one can’t deny a simple fact: it is slow.

No matter how powerful is your laptop and how fast is your host or server – the block editor takes a while to load anyway. Also, with all of its tools and features, it makes us to think about getting rid of part of them, inevitably.

So, like we did with the classic editor in the past, let’s cut some of those useless metaboxes down, shall we? Get all of their names and remove_meta_box() on them, right?

Wrong.

Why can’t we remove it using the same function?

As the classic editor is still around, WordPress core still keeps the remove_meta_box() function around. Also, it serves to other purposes inside the backend. Anyway, the truth is that this old known way of getting rid of the cluttered editor sidebox won’t ever work for Gutenberg’s metaboxes. Why? Because it is not PHP at all.

Thus, any change on the editor needs to occur through JS, rather than PHP. Basically, it would look like this:

wp.data.dispatch( 'core/edit-post').removeEditorPanel( 'post-link' );
wp.data.dispatch( 'core/edit-post').removeEditorPanel( 'page-attributes' );
wp.data.dispatch( 'core/edit-post').removeEditorPanel( 'post-excerpt' );
wp.data.dispatch( 'core/edit-post').removeEditorPanel( 'discussion-panel' );

So, we can create a new Javascript file with these instructions, and then use a PHP function to register and hook the asset on “init” action, so the block editor won’t be loading these panels (or metaboxes, in an old-fashioned vocabulary). The PHP part would look like this:

add_action('init', function () {
  wp_register_script( 'cc-dawn-script',  'url/to/js/file',  array( 'wp-blocks', 'wp-edit-post' ) );
  register_block_type( 'cc/ma-block-files', array( 'editor_script' => 'cc-dawn-script'  ) );
});

Can we make it any more PHP-ish?

Well, we always can do that. One interesting way for accomplish that would be registering a PHP file as script, instead of a JS. This way, we can pass variables to that file and preprocess it before registering, generating the JS script as a result. That would be a way to convey the need of using even more JS files, but considering how easy it is to remove the panels simply using JS, it sounds too much of an effort for getting rid of a few lines of code.

Also, documentation about the block editor customization is still incomplete and the new versions of WP core should be revealing new and easier ways to configure the editor without the need of rebuilds or penetrating within the complexity of its framework.

Leave a comment

Your email address will not be published. Required fields are marked *