Gutenberg: meta_box_cb parameter doesn't work on Post

Created on 11 Feb 2019  路  6Comments  路  Source: WordPress/gutenberg

Describe the bug
Register a taxonomy for a post with a custom panel UI doesn't work with Gutenberg editor but yes with the classic editor. Also, it works with another custom post type.

To Reproduce
Steps to reproduce the behavior:

  1. Register Taxonomy:
<?php
register_taxonomy(
            self::$taxonomy_name,
            array('post'),
            array(
                "meta_box_cb" => [$this,'taxonomy_select_meta_box'],
                'labels' => $labels,
                'public' => true,
                'show_ui' => true,
                'show_in_menu' => false,
                'show_in_rest' => true,
                'hierarchical' => true

            )
        );

 public function taxonomy_select_meta_box( $post, $box ){
        echo "<p>This will show up below the checkboxes that you are used to!</p>";
    }
  1. Go to the edit/create post page:
    The panel UI is not properly:
    image

  2. If I create a new post_type and go to the edit/create the new custom post type, the panel is showing properly:
    image

Desktop (please complete the following information):

  • OS: iOS
  • Browser: chrome
  • Version Version 71.0.3578.98 (Official Build) (64-bit)

    • WordPress 5.0.3

Backwards Compatibility Needs Technical Feedback [Feature] Meta Boxes

Most helpful comment

The meta_box_cb argument is ignore by Gutenberg ( for every post type ).

This is due to the fact that this argument don't show up in the REST API.

I found a small fix to hide the metabox when meta_box_cb is set to false :

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 );

if you want to have your own metabox you should have a look a the editor.PostTaxonomyType filter

All 6 comments

The meta_box_cb argument is ignore by Gutenberg ( for every post type ).

This is due to the fact that this argument don't show up in the REST API.

I found a small fix to hide the metabox when meta_box_cb is set to false :

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 );

if you want to have your own metabox you should have a look a the editor.PostTaxonomyType filter

@ArnaudBan thanks for the response, I will try it.

Hello!
Is there any plan to make the 'meta_box_cb' parameter work again in register_taxonomy even if the post type is a custom one?

I tried to check the Custom Taxonomy Selector as specified by @ArnaudBan but the way to use it is not so obvious (for me at least) after reading the documentation. Is there maybe a better exemple?

Thx!

This is also a problem if you want to change which UI the terms are shown in, despite it's hierarchical nature. Core conflates hierarchical taxonomies with being a fixed list of terms that should be selected with checkboxes, when in reality the two aren't necessarily connected, they just happen to be for tags and categories.

https://core.trac.wordpress.org/ticket/27249
https://core.trac.wordpress.org/ticket/14877

It's possible to have a non-hierarchical taxonomy where the checkbox UI is more appropriate than the text input UI. For example, the tracks in a conference schedule. The track corresponds to a physical room, and in most cases it doesn't really make sense to have a hierarchy.

In that use case, I'd register the taxonomy like this:

'hierarchical' => false,
'meta_box_cb'  => 'post_categories_meta_box',

...so that I don't have a hierarchy, but still get the checkbox UI. That works fine in the Classic Editor, but Gutenberg still displays the text input interface.

...in the previous example, I think post_categories_meta_box should probably be mapped automatically to HierarchicalTermSelector, rather than rendering that panel in PHP using post_categories_meta_box. Same for post_tags_meta_box / FlagTermSelector.

Related: #17476

'meta_box_cb' does not work when registering custom post type with custom taxonomy. Whether it is set to false or callable function it does exactly nothing, neither on quick edit in admin menu, nor in Gutenberg editor UI. No change in meta box.
In my example, I set 'meta_box_cb' to 'post_categories_meta_box', but metabox remains text editor for non-hierarchical items.
// register taxonomy register_taxonomy( 'Stories', 'story_map_posts', array( 'label' => 'Story', 'description' => 'Taxonomy separates different stories', 'public' => true, 'show_in_rest' => true, 'hierarchical' => false, 'meta_box_cb' => 'post_categories_meta_box', 'show_admin_column' => true ) ); } add_action('init', 'story_map_custom_post_type');
image

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nylen picture nylen  路  3Comments

ellatrix picture ellatrix  路  3Comments

davidsword picture davidsword  路  3Comments

moorscode picture moorscode  路  3Comments

spocke picture spocke  路  3Comments