recently I am going through very wierd problem.

What I want:

I attempt to create plugin, which makes user able to create custom defined categories, and then display meta box at add or edit screen for my custom defined content type, called funny_band.

Note:

I went through every possible documentation or tutorial possible. My project also does not contain any futher wierd plugins or theme modifications, I like to keep things simple.

The wierd part:

The same code below I have copied with different naming in another plugin. In first plugin it works, but does not work in the second. Naming clashes are not the case, i checked this for 10 times.

What I tried:

<?php

const MY_PLUGIN__CONTENT_TYPE_NAME = 'funny_band';
const MY_PLUGIN__TAXONOMY = 'funny_bands_categories';

function my_plugin__register_band_type(){

    register_post_type(MY_PLUGIN__CONTENT_TYPE_NAME, [
        'labels' => [
            'name' => __('Bands'),
            'singular_name' => __('Band'),
            'add_new_item' => __('Add new Band'),
        ],
        'menu_icon' => 'dashicons-groups',
        'public' => true,
        'menu_position' => 6,
        'show_in_rest' => true,
        'supports' => ['title', 'editor', 'thumbnail'],
    ]);
}


function my_plugin__register_taxonomies(){

    $labels = [
        'name' => 'Category',
        'singular_name' => 'Category',
    ];

    $category_args = [
        'description' => 'Band categories',
        'public' => true,
        'hierarchical' => false,
        'labels' => $labels,
        'show_admin_column' => true,
        'meta_box_cb' => function($post, $args){
            error_log('This message is not visible!'); 
            echo '<h1>Custom metabox_cb</h1>';
            }
        }
    ];

    register_taxonomy(MY_PLUGIN__TAXONOMY, MY_PLUGIN__CONTENT_TYPE_NAME, $category_args);
    register_taxonomy_for_object_type(MY_PLUGIN__TAXONOMY, MY_PLUGIN__CONTENT_TYPE_NAME);
}

add_action('init', 'my_plugin__register_band_type');
add_action('init', 'my_plugin__register_taxonomies');

Any ideas what can be wrong? I have no idea at all why it does not work.

1 Answer
1

It is not you, it is WordPress. I’ve just spent almost 2 hrs picking my hair debugging the same issue.

Turns out meta_box_cb is only taken into account if the custom post type does NOT use Gutenberg editor – in your case if in the register_post_type() call the 'supports' array would not include 'editor'.

Once Gutenberg is loaded, the metabox-es are not rendered by the meta_box_cb, instead they are rendered by some React components whereas the contents and the config is loaded from the REST API. So that is two entirely different story, and they behave different.

It is rather disturbing that a standard WP PHP API function (register_taxonomy()) produces two entirely different behaviour depending on a parameter somewhere in another call, but this is the case as of v5.6 apparently.

So, if you want to use custom rendering for your metabox for a post that uses the Gutenberg editor, you have to actually build a custom metabox for yourself and register it to your post type and set show_ui to false in the register_taxonomy() parameters, to disable the default UI.

Alternatively you can remove the editor support from the custom post type, and maybe use an other custom metabox based on the wp_editor rich text editor, if you are fine with using regular rich text instead of the block editor.

Or you may disable Gutenberg for the given post type, and let it fall back to the Classic editor.

At least there is a bug about it: https://github.com/WordPress/gutenberg/issues/13816

Leave a Reply

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