How to Remove Taxonomy Meta Boxes from the WordPress Gutenberg Editor Sidebar

Here’s the situation: You’ve created some custom Taxonomies and custom Post Types using the Custom Post Type UI Plugin (or using PHP code).

You’re using Advanced Custom Fields (or another plugin) to manage your Taxonomy settings, so you do NOT want those meta boxes to appear in the sidebar of the WordPress editor. And, you’re using Gutenberg.

I found so many solutions online that didn’t work for me because I was using the Gutenberg editor.

I’ll explain how to hide those fields in the Gutenberg editor in this article.

Pre-Gutenberg

First, a little detour. WordPress has a built-in function which seems like it should do what we want, called remove_meta_box.

Unfortunately, and I confirmed this, it doesn’t work in the WP Gutenberg editor. Further down in the WP repo for remove_meta_box, some user-contributed code using remove_post_type_support that is supposed to work in Gutenberg, but I couldn’t get it to work.

Here’s What Works in Gutenberg

WordPress has a CPT setting called ‘meta_box_cb’ which I’m using to tell some other code to hide these meta boxes. Gutenberg itself ignores this setting, but we’ll use it later.

Setting meta_box_cb to False

If you created your taxonomy using PHP code, set ‘meta_box_cb’ to false in your custom taxonomy settings code.

If you’re using the CPU UI plugin, make sure you’re using at least version 1.6.0, which supports that setting. The very last setting on the Edit Taxonomy page is a field called ‘Metabox callback’. Set that to ‘false’.

If you’re using some other plugin, here’s a way to set that field using filters.

The first filter sets meta_box_cb to false for all taxonomies created by CPT UI:

function tna_edit_taxonomy_args( $args, $tax_slug, $cptui_tax_args ) {
	// Set to false for all taxonomies created with CPTUI.
	$args['meta_box_cb'] = false;
	return $args;
}
add_filter( 'cptui_pre_register_taxonomy', 'tna_edit_taxonomy_args', 10, 3 );

Or, if you only want to hide certain taxonomies, use this:

function tna_edit_taxonomy_args( $args, $tax_slug, $cptui_tax_args ) {
	// Alternatively, you can check for specific taxonomies.
	if ( 'genre' === $tax_slug ) {
		$args['meta_box_cb'] = false;
	}
	return $args;
}
add_filter( 'cptui_pre_register_taxonomy', 'tna_edit_taxonomy_args', 10, 3 );

For more details, check out the original support thread where this code came from.

Hiding the Meta Box

As I mentioned before, the meta_box_cb to false is ignored by Gutenberg. However, we can add some code to use it to hide our meta boxes:

add_filter( 'rest_prepare_taxonomy', function( $response, $taxonomy, $request ){
	$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
	// Context is edit in the editor
	if( $context === 'edit' && $taxonomy->meta_box_cb === false ){
		$data_response = $response->get_data();
		$data_response['visibility']['show_ui'] = false;
		$response->set_data( $data_response );
		}
	return $response;
}, 10, 3 );

For more details, check out the original post for this. Enter this code in functions.php of your child theme.

Conclusion

So, this should hide any custom taxonomies from your Gutenberg editor sidebar. Let me know if it worked for you, or if you have questions! – Brian



Leave a Comment or Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.