Hi Guys i found this code snippet on here:
add_action( 'set_user_role', function( $user_id, $role, $old_roles )
{
// Your code ...
}, 10, 3 );
Source: Execute a function when admin changes the user role
And wanted to modify it it so that it could set the user roles in the following way:
1 user has roles "level0" and "customer" -> roles "level1" and "vendor"
2 user has roles "level1" and "vendor" -> roles "level0" and "customer"
In case the admin is changing the role "level0" to "level1" the code should update the second role from "customer" to "vendor" and vice versa.
i tried the following code but it is not working:
add_action( 'set_user_role', 'rb_update_user_role',10,3);
function rb_update_user_role(){
$user_id = get_current_user_id();
$user = new WP_User ($user_id);
if (current_user_is('s2member_level0') && current_user_is('vendor')){
$user->remove_role('vendor');
$user->add_role('customer');
}
if(current_user_is('s2member_level1') && current_user_is ('customer')){
$user->remove_role('customer');
$user->add_role('vendor');
}
}
Has anyone suggestions or code snippets that could help me figure this out? Or do i need another approch on this?
EDIT:
I add the second role like this when the user is activated
add_action( 'bp_core_activated_user', 'add_secondary_role_new', 10, 1 );
function add_secondary_role_new( $user_id ) {
global $members_template;
$user = get_user_by('id', $user_id);
if (user_can($members_template->member->id,'s2member_level1')){
$user->add_role('vendor');
}
else{
$user->add_role('customer');
}
}