Question

I've got a custom taxonomy, is there a way to only show it in the dashboard if the current user is an admin? I need something along the lines of this, but it's not working:

 global $current_user;
 global $showui;
     if($current_user->roles[0] == 'administrator') {
       $showui = true;
     }  
     else {
           $showui = false;
          }
//Custom Industry Taxonomy Code (For Projects CPT)
register_taxonomy('servicecategory',
    array ( 0 => 'servicecategory',),
    array( 'hierarchical' => true, 
            'label' => 'Specialties',
            'show_ui' => $showui,
            'query_var' => true,
            'rewrite' => true,
            'show_in_nav_menus' => true,
            'show_admin_column' => true,                
            'singular_label' => 'Menu'
        ) 
    );  
Was it helpful?

Solution

You can first name the capability of managing this taxonomy. Then add the capability to the administrator, like this:

register_taxonomy('servicecategory',
    array ( 0 => 'servicecategory',),
    array( 'hierarchical' => true, 
            'label' => 'Specialties',
            'show_ui' => true,
            'query_var' => true,
            'rewrite' => true,
            'show_in_nav_menus' => true,
            'show_admin_column' => true,                
            'singular_label' => 'Menu',
            'capabilities' => array ( //giving a name to the capability
                'manage_terms' => 'manage_servicecategory', 
                'edit_terms' => 'manage_servicecategory',
                'delete_terms' => 'manage_servicecategory',
                'assign_terms' => 'manage_servicecategory'
            )
    ) 
); 
$role = get_role('administrator');
$role->add_cap("manage_servicecategory");

OTHER TIPS

Untested but try this adding this to only show for admins

'show_ui' => current_user_can( 'update_core' )

This restricts the ui to only those with the capabilites of updating wordpress, which is always going to be admins.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top