Custom taxonomies capabilities

admin2025-01-08  4

I have registered several custom taxonomies for a custom post type. Code for one of them is the following:

$labels = array(
    'name'              => __( 'Genre', 'textdomain' ),
    'singular_name'     => __( 'Genre', 'textdomain' ),
    'search_items'      => __( 'Search Genres', 'textdomain' ),
    'all_items'         => __( 'All Genres', 'textdomain' ),
    'parent_item'       => __( 'Parent Genre', 'textdomain' ),
    'parent_item_colon' => __( 'Perent Genre:', 'textdomain' ),
    'edit_item'         => __( 'Edit Genre', 'textdomain' ), 
    'update_item'       => __( 'Update Genre', 'textdomain' ),
    'add_new_item'      => __( 'Add New Genre', 'textdomain' ),
    'new_item_name'     => __( 'New Genre', 'textdomain' ),
    'menu_name'         => __( 'Genres', 'textdomain' ),
);
$args = array(
    'labels'            => $labels,
    'public'            => true,
    'show_admin_column' => true,
    'show_ui'           => true,
    'hierarchical'      => true,
    'capabilities'      => array(
        'manage_terms'  => 'edit_posts',
        'edit_terms'    => 'edit_posts',
        'delete_terms'  => 'edit_posts',
        'assign_terms'  => 'edit_posts'
    )
);
register_taxonomy( 'genres', 'book', $args );

I'm having issues with capabilities, as far as I want that one of the custom tax could be managed by authors.

If I define capabilities as the code above like 'manage_terms' => 'edit_posts', authors are not allowed to add new terms to the custom taxonomy even when authors have edit_posts capabilities.

I have also tried to add custom capabilities to author role and assign this capabilities when registering the custom taxonomy, but no success.

What does work is add 'manage_categories' to authors role, but this solution is not satisfactory as far as all the taxonomies are then editable by authors.

Thanks in advance.

I have registered several custom taxonomies for a custom post type. Code for one of them is the following:

$labels = array(
    'name'              => __( 'Genre', 'textdomain' ),
    'singular_name'     => __( 'Genre', 'textdomain' ),
    'search_items'      => __( 'Search Genres', 'textdomain' ),
    'all_items'         => __( 'All Genres', 'textdomain' ),
    'parent_item'       => __( 'Parent Genre', 'textdomain' ),
    'parent_item_colon' => __( 'Perent Genre:', 'textdomain' ),
    'edit_item'         => __( 'Edit Genre', 'textdomain' ), 
    'update_item'       => __( 'Update Genre', 'textdomain' ),
    'add_new_item'      => __( 'Add New Genre', 'textdomain' ),
    'new_item_name'     => __( 'New Genre', 'textdomain' ),
    'menu_name'         => __( 'Genres', 'textdomain' ),
);
$args = array(
    'labels'            => $labels,
    'public'            => true,
    'show_admin_column' => true,
    'show_ui'           => true,
    'hierarchical'      => true,
    'capabilities'      => array(
        'manage_terms'  => 'edit_posts',
        'edit_terms'    => 'edit_posts',
        'delete_terms'  => 'edit_posts',
        'assign_terms'  => 'edit_posts'
    )
);
register_taxonomy( 'genres', 'book', $args );

I'm having issues with capabilities, as far as I want that one of the custom tax could be managed by authors.

If I define capabilities as the code above like 'manage_terms' => 'edit_posts', authors are not allowed to add new terms to the custom taxonomy even when authors have edit_posts capabilities.

I have also tried to add custom capabilities to author role and assign this capabilities when registering the custom taxonomy, but no success.

What does work is add 'manage_categories' to authors role, but this solution is not satisfactory as far as all the taxonomies are then editable by authors.

Thanks in advance.

Share Improve this question edited Feb 21, 2019 at 17:53 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Jul 24, 2014 at 9:16 vguerrerovguerrero 5551 gold badge4 silver badges9 bronze badges 1
  • wordpress.stackexchange.com/questions/155629/… – vguerrero Commented Feb 21, 2019 at 23:25
Add a comment  | 

3 Answers 3

Reset to default 9

Just like CPT capabilities those of taxonomy are also customizable, in register_taxonomy():

capabilities

  • 'manage_terms' - 'manage_categories'
  • 'edit_terms' - 'manage_categories'
  • 'delete_terms' - 'manage_categories'
  • 'assign_terms' - 'edit_posts'

Since your authors only have edit_posts it works as you observe — they can assign existing terms, but not create them. You can customize capabilities for taxonomy in question and give respective capability to authors, so that they can create terms in it (but not in other taxonomies).

When you are defining capabilities while registering a taxonomy you are defining new capabilities that are equal to the existing capabilities of manage_terms edit_terms delete_terms and assign_terms.

So you should do something like this since your taxonomy is Genre:

'capabilities' => array(
    'manage_terms' => 'manage_genre',
    'edit_terms' => 'edit_genre',
    'delete_terms' => 'delete_genre',
    'assign_terms' => 'assign_genre',
)

After this is done, you will be able to map these capabilities to a user role. The easiest way to do this and my preferred method is to use a plugin such as Capability Manager Enhanced, although there are many plugins that will do the trick.

In case anyone ends up here looking to modify a previously registered taxonomy, you can use the register_taxonomy_args.

add_filter( 'register_taxonomy_args', 'modify_my_taxonomy', 20, 2 );
function modify_my_taxonomy( $args, $taxonomy ) {
    $capabilities = array(
        'manage_terms' => 'edit_posts',
        'edit_terms'   => 'edit_posts',
        'delete_terms' => 'edit_posts',
        'assign_terms' => 'edit_posts',
    );

    
    if ( 'my-tax' === $taxonomy ) {
        $args['capabilities'] = $capabilities;
    } 

    return $args;

}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736268034a1242.html

最新回复(0)