1

I am only just learning about custom taxonomies for Wordpress. How is it possible to restrict access for my users to use a taxonomy. For instance, I have created a taxonomy named featured and I only want Editors and above roles to be able to add posts to this taxonomy.

How do I set the access level? Either based on user role or capability, both works for me.

Here is the code that I use for my taxonomy:

function add_custom_taxonomies() {
    // Add new "Featured" taxonomy to Posts
    register_taxonomy('featured', 'post', array(
        // Hierarchical taxonomy (like categories)
        'hierarchical' => true,
        // This array of options controls the labels displayed in the WordPress Admin UI
        'labels' => array(
            'name' => _x( 'Featured', 'taxonomy general name' ),
            'singular_name' => _x( 'Featured', 'taxonomy singular name' ),
            'search_items' =>  __( 'Search Featured' ),
            'all_items' => __( 'All Featured' ),
            'parent_item' => __( 'Parent Featured' ),
            'parent_item_colon' => __( 'Parent Featured:' ),
            'edit_item' => __( 'Edit Featured' ),
            'update_item' => __( 'Update Featured' ),
            'add_new_item' => __( 'Add New Featured' ),
            'new_item_name' => __( 'New Featured Name' ),
            'menu_name' => __( 'Featured' ),
        ),
        // Control the slugs used for this taxonomy
        'rewrite' => array(
            'slug' => 'featured', // This controls the base slug that will display before each term
            'with_front' => false, // Don't display the category base before "/locations/"
            'hierarchical' => true // This will allow URL's like "/locations/boston/cambridge/"
        ),
    ));
}
add_action( 'init', 'add_custom_taxonomies', 0 );
Gary Woods
  • 1,011
  • 1
  • 15
  • 33

1 Answers1

2

Would it be sufficient to remove the metabox from the Post Edit page? If so, give this a whirl:

function remove_featured_meta() {
   if (!current_user_can('moderate_comments')){
       remove_meta_box( 'featureddiv', 'post', 'side' );
   }
}

add_action( 'admin_menu' , 'remove_featured_meta' );

You can fill in the conditional statement with whichever appropriate capability corresponds to the user role to which you'd like to limit access to the taxonomy.

crowjonah
  • 2,858
  • 1
  • 23
  • 27
  • Interesting approach. If I understand this correctly, all users that can **moderate_comments** will be able to view the metabox in post edit? – Gary Woods Sep 17 '12 at 18:03
  • Correct! And as seen in [the WP docs](http://codex.wordpress.org/Roles_and_Capabilities), only Editors and above are able to do so by default. – crowjonah Sep 17 '12 at 18:05
  • Excellent, great answer. I just have one final question, why is it that that there is no core functions to set access levels? I would presume that this is something not that unusual, don't you think? – Gary Woods Sep 17 '12 at 18:13
  • 1
    I could be wrong, but my guess is that since Wordpress has expanded so much as a CMS (and wasn't originally conceived of as having custom post types and taxonomies,) access levels as taxonomy parameters weren't accounted for. There are plenty of ways to test user capabilities, though, and prevent them from changing things you'd not like them to. From a taxonomy perspective, it'd make sense to let users who can add tags and categories change other terms, so it's up to us to work through the exceptions when we're leaning on those things for templating and theming. – crowjonah Sep 17 '12 at 18:21