Shold I manually add ‘cap’ to admin role ?

I’ve created a role:

$role = add_role( 'role', 'role', array('read' => true) );

and so created a cap:

$role->add_cap( 'cap' );

after this I’d created a submenu in admin menu with ‘cap’ as capability argument.
So the problem is that the user with ‘role’ role can see the submenu but the site admin can’t see the submenu, so the question is: I have to add ‘cap’ to admin role ? isn’t it automatically ?

1 Answer
1

A new capability has to be explicitly added to either a role or a user. In your case if you want all administrators to have ‘cap’ capability you will add it to ‘administrator’ role:

$role = get_role( 'administrator' );
$role->add_cap( 'cap' );

If you want a specific administrator only to have ‘cap’ capability then you add it to this particular user:

$user = new WP_User( $user_id ); // $user_id = id of your administrator user
$user->add_cap( 'cap' );

Leave a Comment