How can I allow a custom taxonomy for certain roles?

Im trying to only show a taxonomy for the editor and administrator but I haven’t found a solution on SO or here. The closest thing I was able to find was How do I remove a taxonomy from WordPress? on SO. What is the proper way to only allow the taxonomy for the editor and administrator?

// remove from everyone that isn't admin or editor
function taxonomy_for_admin_and_editor_only() {
   if ( !current_user_can( 'administrator' ) || !current_user_can( 'editor' ) ) {
       register_taxonomy( 'foobar', array() );
   }
}
add_action( 'init' , 'taxonomy_for_admin_and_editor_only' );

I’ve tried add_action( 'admin' , 'taxonomy_for_admin_and_editor_only' ); with no luck either.

3 Answers
3

When registering your taxonomy you can pass an argument called capabilities. Simply passing capabiities that only admins and editors have.

$args = array(
    'capabilities' => array( 'manage_options', 'edit_posts' )
);
register_taxonomy( 'foobar', 'post', $args ); 

https://codex.wordpress.org/Roles_and_Capabilities#Editor

Leave a Comment