Alright, so i have a custom post type called Blurbs (translated, sort of). I want to add a static set of categories for the posts within Blurbs (i don’t want the user to be able to create custom categories). I want two different types of categories; links and page-excerpts. I did some reading and came to the conclusion that taxonomies is the way to go for adding these “categories” to the post type.
I got as far as creating the taxonomy, but i don’t want it visible in the menu. I just want the “category”-box on the edit/publish screen, with the two static items.
This is what i have so far:
$labels = array(
'name' => 'Categories',
'singular_name' => 'Category',
'search_items' => 'Search categories',
'all_items' => 'All categories',
'edit_item' => 'Change category',
'update_item' => 'Update category',
'add_new_item' => 'Create new category',
'new_item_name' => 'New category name'
);
register_taxonomy('categories', array('blurb'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
));
By setting the show_ui
to false
, it disappears from both places (menu and links-box).
So, the two questions i need help with is:
- How do i remove it from the menu?
- How do i give it “static” options (aka categories) to choose from?
Make it
show_ui => false
Then to show it on the post edit screen add the box manually
use this code for every static term
There’s a much simplier (and more secure way*) way than hiding the user interface (
show_ui=false
) and adding a custom metabox to only display terms: removing the user’s ability to manage terms.If you remove the capability of the user to manage terms not only do you have a secure solution, but user interface takes care of itself. As part of the
register_taxonomy()
you can specify the capabilities the user must have to manage/edit/delete and assign terms.For the first three you’ll want to set the capability to something the user doesn’t have. In fact, leaving it as above will probably do. If you still want to manage/edit/delete terms you can always use a capability that you have but your client does (can they
manage_options
, for instance?). That will allow you to create and maintain the ‘static’ list. Or you can simply do that before you make the above changes.Lastly you’ll want to give a capability to
assign_terms
the user does have. By default, it isedit_posts
, so you’re probably ok to leave it at that. However you may want to create a new capabilityedit_blurb
, so that you can allow your user to edit blurbs, but not posts.WordPress, then handles the rest. As the user cannot manage/edit/delete terms, the admin menu is gone. Furthermore the metabox on the edit blurb page displays only existing terms, and the user cannot add/remove or edit any.
*Remember that hiding UI doesn’t remove the user’s ability to edit, and delete terms, it just hides it.
You should first use the menu to get to the taxonomy management page to add the static values you want to add, then remove it from public consumption.
From:
http://codex.wordpress.org/Function_Reference/register_taxonomy
Use the show_in_nav_menus variable in your taxonomy arguments, i.e.:
This code runs “wp_insert_term” only when needed (only when querying for all terms) for better performances