add_cap only adding the first two in array

Looking at your code I think you are trying to modify the capabilities of existent roles. Am I right?

To do that you have to run the add_cap for a role object, not for a user object. Also, you must know that add_cap only accept one capability as string:

$role = get_role( 'editor' );
$role->add_cap( 'the_capability' );

If you want to add more than one capability you can perform a loop. For example:

$capabilities = array( 'cap_1', 'cap_2', 'cap_3' );
$role = get_role( 'editor' );

foreach( $capabilities as $cap ) {
        $role->add_cap( $cap );
}

The same apply to perform add_cap for a user object:

//example of user ID
$user_id = 25
$capabilities = array( 'cap_1', 'cap_2', 'cap_3' );
$user = new WP_User( $user_id );

foreach( $capabilities as $cap ) {
        $user->add_cap( $cap );
}

Leave a Comment