Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to make custom taxonomy appear on Gutenberg-enabled post types? #11106

Closed
deckerweb opened this issue Oct 26, 2018 · 14 comments
Closed

How to make custom taxonomy appear on Gutenberg-enabled post types? #11106

deckerweb opened this issue Oct 26, 2018 · 14 comments
Labels
[Type] Help Request Help with setup, implementation, or "How do I?" questions.

Comments

@deckerweb
Copy link

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:

  1. Do I rather need my taxonomy remove from the stripping out of Gutenberg?
    or:
  2. 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 deckerweb changed the title How to custom taxonomy appear on Gutenberg-enabled post types? How to make custom taxonomy appear on Gutenberg-enabled post types? Oct 26, 2018
@chrisvanpatten
Copy link
Member

@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?

@chrisvanpatten chrisvanpatten added [Type] Help Request Help with setup, implementation, or "How do I?" questions. [Status] Needs More Info Follow-up required in order to be actionable. labels Oct 26, 2018
@deckerweb
Copy link
Author

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...

@chrisvanpatten
Copy link
Member

chrisvanpatten commented Oct 26, 2018

@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…

@chrisvanpatten
Copy link
Member

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:

const visibleTaxonomies = filter( availableTaxonomies, ( taxonomy ) => taxonomy.visibility.show_ui );

And you can see that the only check there is for taxonomies with show_ui = true.

@deckerweb
Copy link
Author

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)

@chrisvanpatten
Copy link
Member

@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.

@deckerweb
Copy link
Author

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

@chrisvanpatten
Copy link
Member

@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.

@deckerweb
Copy link
Author

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
Copy link
Author

@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.

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

@deckerweb
Copy link
Author

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

@designsimply designsimply removed the [Status] Needs More Info Follow-up required in order to be actionable. label Nov 29, 2018
@designsimply designsimply added [Block] Archives Affects the Archives Block and removed [Block] Archives Affects the Archives Block labels Nov 29, 2018
@couponrim
Copy link

I'm looking for Gutenberg works with custom post types or Post Category, post tags

@designsimply
Copy link
Member

@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.

@couponrim
Copy link

@designsimply
Thank you so much for your helps. I'm checking it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Help Request Help with setup, implementation, or "How do I?" questions.
Projects
None yet
Development

No branches or pull requests

4 participants