How to assign multiple roles for capabilities array withini register_taxonomy function?

The question:

I am not sure how to add multiple capabilities to ‘assign_terms’ capability.

My code is below, notice the capabilities array within the register_taxonomy function

/* Registers taxonomies. */

function cflnk_wod_score_register_taxonomies() {
/* Set up the artist taxonomy arguments. */
$wod_name_args = array(
    'hierarchical' => false,
    'query_var' => 'wod_name', 
    'show_tagcloud' => true,
        'rewrite' => array(
            'slug' => 'wod/name',
            'with_front' => false 
            ),
    here --->>> 'capabilities' => array (
            'manage_terms' => 'administrator',
            'edit_terms' => 'administrator',
            'delete_terms' => 'administrator',
            'assign_terms' => 'administrator', 'editor', 'author', 'contributor'
            ),
    'labels' => array(
        'name' => 'WOD Names',
        'singular_name' => 'WOD Name',
        'edit_item' => 'Edit WOD Name',
        'update_item' => 'Update WOD Name',
        'add_new_item' => 'Add New WOD Name',
        'new_item_name' => 'New WOD Name Name',
        'all_items' => 'All WOD Names',
        'search_items' => 'Search WOD Names',
        'popular_items' => 'Popular WOD Names',
        'separate_items_with_commas' => 'Separate wod names with commas',
        'add_or_remove_items' => 'Add or remove wod names',
        'choose_from_most_used' => 'Choose from the most popular wod names',
    ),
);

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

When assigning capabilities to ‘capabilities’ argument of register_taxonomy() you need to assign the capability and not the role! so use capabilities that only a specific role has eg:

'capabilities' => array (
            'manage_terms' => 'manage_options', //by default only admin
            'edit_terms' => 'manage_options',
            'delete_terms' => 'manage_options',
            'assign_terms' => 'edit_posts'  // means administrator', 'editor', 'author', 'contributor'
            )


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment