I'm trying to implement a way to add multiple roles at once with php and wordpress. See image below.
The way it works is that I can select multiple roles and get their "slugs". Everything works so far and I get the correct role "slug" out. Now the next issue is when I go to php and update the roles for the user. I use a foreach on the array i create with selected roles and use add_role() in this foreach. This foreach however only adds the first role for some reason and skips the others. Any ideas for why it would only use the first role selected even though all data is correct?
This is the array I send to my php:

And this would be my php that should update the users role:
$oUser = get_user_by('ID', $_POST['iUserID']);
if(isset($_POST['aRoles']) && !empty($_POST['aRoles'])){
foreach ($_POST['aRoles'] as $key => $oRole) {
$oUser->add_role( $oRole );
}
}
I've tried:
- Adding sleep to see that maybe the code was running too fast
- I've made sure that the code does indeed reach inside the foreach
- Tried manually setting the roles string, no luck.

php $oUser->remove_role( $rolename ); $oUser->add_role( $rolename );- Lato