0

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');
   }

}

  • @birgire is it possible that you have a suggestion since it was your answer on the post i referenced? – Bexx Jan 11 '17 at 11:01
0

Because you are operating on the current (logged in) user - you need to accept parameters in your callback so that you can operate on the user whose role is being set:

rb_update_user_role( $user_id, $role ) {
    // error_log( "Setting user $user_id to role $role" );

    if ( $role !== 's2member_level0' && $role !== 's2member_level1' )
        return;

    $user = new WP_User( $user_id );

    if ( $role === 's2member_level0' ) {
        $user->remove_role( 'vendor' );
        $user->add_role( 'customer' );
    } else {
        $user->remove_role( 'customer' );
        $user->add_role( 'vendor' );
    }
}

add_action( 'set_user_role', 'rb_update_user_role', 10, 2 );

Note you do not need to remove any roles - WP_User::set_role explicitly removes all other roles before it fires the action.

  • that worked but now i got for instance an user with level1, customer and vendor but it should only be level1&vendor. so i kind of need to remove the role don't i? @TheDeadMedic – Bexx Jan 11 '17 at 12:02
  • That shouldn't be the case. I've removed it anyway. – TheDeadMedic Jan 11 '17 at 12:12
  • hm tried it and it still shows me all 3 roles like "level1,vendor,customer" that's strange isn't it? – Bexx Jan 11 '17 at 12:16
  • Can you confirm that the action is definitely firing? Try enabling debug logging, then add my debugging code without the comments (see revision), then check your wp-content/debug.log file after running a test. – TheDeadMedic Jan 11 '17 at 12:19
  • [11-Jan-2017 12:53:08 UTC] Setting user 105 to role s2member_level1 that is what the debug log says – Bexx Jan 11 '17 at 12:54
0

I found a solution. Here it is:

//Update Roles automatically when Membership Level changes
function rb_update_user_role( $user_id) {
    $user = new WP_User( $user_id );
    $user_meta=get_userdata($user_id);
    $all_role = $user_meta->roles;

    foreach ($all_role as $role_value){
        if ($role_value === 'vendor'){
           $user->remove_role( 'vendor' );
        }
        if ($role_value === 'customer'){
           $user->remove_role( 'customer' );
        }
        if ($role_value === 's2member_level1'){
           $user->remove_role( 's2member_level1' );
           $role = 's2member_level1';
        }
        if ($role_value === 'subscriber'){
           $user->remove_role( 'subscriber' );
           $role = 'subscriber';
        }
    }
    if ( $role === 'subscriber' ) {
       unset($new_role);
       $new_role = array($role => 0 ,'customer' => 1) ;
       update_user_meta($user_id, 'wp_capabilities', $new_role);
    } 

    if ($role === 's2member_level1'){
       unset($new_role);
       $new_role = array($role => 0,'vendor' => 1) ;
       update_user_meta($user_id, 'wp_capabilities', $new_role);
    } 
}
add_action( 'profile_update', 'rb_update_user_role', 99, 1 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.