Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

1) How do i remove it from the menu? 2) How do i give it "static" options (aka categories) to choose from?

share|improve this question

4 Answers 4

up vote 1 down vote accepted

1) Make it show_ui => false

Then to show it on the post edit screen add the box manually

add_action('add_meta_boxes', 'meta_boxes_function');
function meta_boxes_function() {
    add_meta_box('categoriesdiv', 'categories', 'post_categories_meta_box', 'blurb', 'side', null, array( 'taxonomy' => 'categories' ));
}

2) use this code for every static term

if(!term_exists('term1', 'categories'))
    wp_insert_term('term1', 'categories');
share|improve this answer
    
Isn't it smarter to keep show_ui=true and just disable the menu option by setting show_in_nav_menus to false? Also, about the wp_insert_term(), where do i place that code? Do i need to run it on every load (if so, should i place it in functions.php?) or just once to add the "category" to the db? And after adding the categories, how do i disable the option for users to add new categotries? - EDIT: Just saw Macgyver's update, show_in_nav_menus won't work, i'll go with your solution on that one. –  qwerty Oct 18 '12 at 14:45
    
'show_ui' => false would be enough to not show it to the user even if they type the url directly in the address bar, hiding from menu won't protect against this. The code basically checks if the term exists & insert it if not. You may run it once or as many times as you want, but if you can just remove the code once the term is added to the DB( for better performance) –  Mridul Aggarwal Oct 18 '12 at 14:50
    
Seems good then! I can't test it right now but i'll test it as soon as again, which will be tomorrow. I'll post back to let you know how/if it worked. Thanks bud! –  qwerty Oct 18 '12 at 14:52
    
The box still has the "Create new category" link below the box, is it possible to get rid of that? –  qwerty Oct 19 '12 at 7:11
1  
Yes, just search wp directory for the function post_categories_meta_box. Copy the function to theme's functions.php, rename the function, modify the html to remove that link. Then in the above code, replace the function name to your new function name –  Mridul Aggarwal Oct 19 '12 at 7:32

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.

If you remove the capability of the user to manage terms, however, 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.

 register_taxonomy( 
    'categories',
    array( 'blurb' ),
    array( 
      ...
      'show_ui' => true,
      'capabilities' => array(
        'manage_terms' => 'a_capability_the_user_doesnt_have',
        'edit_terms'   => 'a_capability_the_user_doesnt_have',
        'delete_terms' => 'a_capability_the_user_doesnt_have',
        'assign_terms' => 'edit_post'
      ),
      ...
    )
 )

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 is edit_post, so you're probably ok to leave it at that. However you may want to create a new capability edit_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.

share|improve this answer

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

register_taxonomy('categories', array('blurb'), array(
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'show_in_nav_menus' => false,
      ));
share|improve this answer
    
Looks good! That solves one of my problems, my second problem is creating "static" categories, and disabling the option to create new ones. –  qwerty Oct 18 '12 at 14:42
    
this won't work. show_in_nav_menus handles if they are available for adding in the navigation menus, not the wordpress admin menu –  Mridul Aggarwal Oct 18 '12 at 14:44
    
Ah crap, hopefully Mdiruls answer works better! –  qwerty Oct 18 '12 at 14:46

This code runs "wp_insert_term" only when needed (only when querying for all terms) for better performances

register_taxonomy(
  'categories',
  null,
  array(
    'hierarchical' => true,
    'labels' => array(
      'name' => 'Categories'
    ),
    'show_admin_column' => true,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array('slug' => 'categories'),
  )
);

function create_default_taxonomies_values ($terms, $taxonomies, $args) {
  if ($args['get'] != 'all') return $terms;
  if (empty($taxonomies[0]) || $taxonomies[0] != 'categories') return $terms;

  // default values here:
  $default_values = array('Value 1', 'Value 2');

  foreach ($default_values as $value) {
    if (!term_exists($value, 'categories')) {
      wp_insert_term($value, 'categories');
      return get_terms($taxonomies, $args);
    }
  }

  return $terms;
}
add_filter('get_terms', 'create_default_taxonomies_values', 10, 3);
share|improve this answer
    
This would ensure that the default values always existed, but it wouldn't stop a user editing them or creating new ones (which I believe is what qwerty was after). If you could prevent a user from editing/adding/removing terms, you could create the default terms manually and leave it at that - that would save having to check the term exists on every page load. –  Stephen Harris 6 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.