How to make custom taxonomy appear on Gutenberg-enabled post types?
Hi there! How can I make a custom taxonomy appear on Gutenberg-enabled post types?
- the custom taxonomy already has Rest API enabled at registering
- this taxonomy is "only" needed internally for organizing purposes (users set it on editing a post/post type, and then can filter for taxonomies in the post type overview table)
- I am totally fine with either result: a Gutenberg-powered JS type of "meta box" or the classic legacy meta box which Gutenberg can/should render...
I tried a lot of code snippets in the last weeks but none of them worked (I guess most were a few months old, and Gutenberg changed things in the last weeks?). Then I did find out that Gutenberg in the whole area of "Meta Box" support strips out meta boxes from custom taxonomies.
This is really bad and should not be the case. I found they have a filter available but I cannot cope with it currently as their used terminology is not entirely clear to me at what this filter really does and how:
- Do I rather need my taxonomy remove from the stripping out of Gutenberg? or:
- Do I need to explicitely add my taxonomy somewhere so Gutenberg will render the "legacy" meta box?
In general I am fine in working with filters as I do plugins myself ;-) However in this special case I am totally struggleing with it as it is not clear how to me how Gutenberg works in this area.
Thanks for any tips and help to point me in the right direction!
@deckerweb it should work without any special changes if you support show_in_rest and public. Can you share the code you're using to register your taxonomy?
It is part of my plugin "Builder Template Categories" and here's the register function: https://github.com/deckerweb/builder-template-categories/blob/f8b386f18f03ef28ca60014de8a7bdc18f259bfa/includes/register-taxonomy.php#L121
Ok, you are saying it has to be also "public" -- which is currently not, as this taxonomy is only intended for internal purposes to organize things, mmh don't know how to go forward then...
@deckerweb You can also turn show_ui on, which should make it so the UI is visible but the taxonomy remains "private".
EDIT: I see you did that already. Let me play around and see…
So one thing to check would be the taxonomies endpoint of the REST API (/wp-json/wp/v2/taxonomies), and see what the show_ui property is returning for your custom taxonomy.
The code that adds the taxonomy panels is here: https://github.com/WordPress/gutenberg/blob/8517779feddf3de0b86465fae8f21f4384447c58/packages/editor/src/components/post-taxonomies/index.js#L21
And you can see that the only check there is for taxonomies with show_ui = true.
Thanks for the feedback! :) For testing purposes you might want to add "Pages" to my taxonomy, here's a Gist for that: https://gist.github.com/deckerweb/dd00884e81783e7d21046ae0a72ed683 (The plugin conditionally adds integrations, or custom ones)
I already played around with "public" => TRUE but it makes no difference as of now (no meta box for tax)
@deckerweb the only other thing I noticed is that you don't appear to be setting a post type for the taxonomy:
https://github.com/deckerweb/builder-template-categories/blob/f8b386f18f03ef28ca60014de8a7bdc18f259bfa/includes/register-taxonomy.php#L129
The editor will only show taxonomies that are enabled for the post type, so you might just need to register the support for your type.
Yes, the adding of a post type happens conditionally for any integration (of template type plugins with a post type...) --- which works like a charm
@deckerweb if you're not explicitly setting the post type relations inside your register_taxonomy method call I think you want to be passing null, not array(), as the second attribute.
This is what I get for the tax endpoint - the output is identical for show_ui TRUE or FALSE:
{"category":{"name":"Kategorien","slug":"category","description":"","types":["post"],"hierarchical":true,"rest_base":"categories","_links":{"collection":[{"href":"http:\/\/gutenberg.local\/wp-json\/wp\/v2\/taxonomies"}],"wp:items":[{"href":"http:\/\/gutenberg.local\/wp-json\/wp\/v2\/categories"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}},"post_tag":{"name":"Schlagw\u00f6rter","slug":"post_tag","description":"","types":["post"],"hierarchical":false,"rest_base":"tags","_links":{"collection":[{"href":"http:\/\/gutenberg.local\/wp-json\/wp\/v2\/taxonomies"}],"wp:items":[{"href":"http:\/\/gutenberg.local\/wp-json\/wp\/v2\/tags"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}},"builder-template-category":{"name":"-Kategorien","slug":"builder-template-category","description":"Template-Kategorien f\u00fcr Templates von Page Buildern oder \u00e4hnlichen Bibliotheken","types":[],"hierarchical":true,"rest_base":"builder-template-category","_links":{"collection":[{"href":"http:\/\/gutenberg.local\/wp-json\/wp\/v2\/taxonomies"}],"wp:items":[{"href":"http:\/\/gutenberg.local\/wp-json\/wp\/v2\/builder-template-category"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}}
@deckerweb if you're not explicitly setting the post type relations inside your
register_taxonomymethod call I think you want to be passingnull, notarray(), as the second attribute.
Ok, when passing null instead of empty array, it's still not working
But when explicitely registering for a post type like page then it works - and the Gutenberg type tax box appears! ;-)
Maybe I can build a workaround that way for the special use case of the plugin. Your input helps me a lot already, thanks so much @chrisvanpatten
@chrisvanpatten Thanks again for your feedback and pointing me in the right direction! I found a working solution which also works with my conditionally adding of the tax to certain post types. I added a special optional key in the integration registering which controls if a post type will be explicitely added on registration of the taxonomy. For me this issue is solved for now! ;-)
I'm looking for Gutenberg works with custom post types or Post Category, post tags
@couponrim I noticed this issue mentions a plugin which is registering taxonomies and I thought maybe it could help you as a working example if you look at that code:
https://github.com/deckerweb/builder-template-categories/blob/master/includes/register-taxonomy.php
I found this which explains how to make a custom post type work with Gutenberg:
https://wordpress.stackexchange.com/questions/324221/enable-gutenberg-on-custom-post-type
I'm not sure where to point you for information about "Post Category, post tags" and would like to suggest that you should ask for help either at https://wordpress.org/support/forum/wp-advanced/ or at https://wordpress.stackexchange.com/ to start because those places are more appropriate for help requests while GitHub repositories like this one are generally more restricted in terms of posting bug reports and feature requests, especially after the Gutenberg code merged to WordPress Core in WordPress 5.0, and those support resources should also be able to help you sort out how to ask more detailed questions and include code examples for things you've already tried if you have already tried anything. To successfully get help with such requests, it's really good to include details such as a short explanation of exactly what you're trying to do, what you've tried already, and any code examples that you have either already tried or that you have reviewed and are wanting to learn more about.
@designsimply Thank you so much for your helps. I'm checking it.