Hi,
Thank you.
I will reindex the array of roles granted to user after the modification of the list of roles/capabilities granted to a user.
Hi again,
Looking more thoroughly, I remembered that user->roles array indexing does not depend from the User Role Editor plugin. Look how WordPress builds the array of roles granted to user (file wp-includes/class-wp-user.php, function get_role_caps(), line #507:
// Filter out caps that are not role names and assign to $this->roles.
if ( is_array( $this->caps ) ) {
$this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) );
}
If you look at the php.net for the array_filter() function documentation, you find, that
Array keys are preserved, and may result in gaps if the array was indexed. The result array can be reindexed using the array_values() function.
So if user has subscriber role, but it is 5th in the $wp_roles array, resulting $user->roles array will be looked this way array(4=>'subscriber').
So primary role at the user’s roles array not always have the ‘0’ index. We have to use another way to get the 1st element of the roles array, than just a $user->roles[0].