Skip to main content
added 66 characters in body
Source Link
TheDeadMedic
  • 36.6k
  • 9
  • 68
  • 102

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.

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 ) {
    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.

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.

added 100 characters in body
Source Link
TheDeadMedic
  • 36.6k
  • 9
  • 68
  • 102

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 ) {
    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.

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 ) {
    if ( $role !== 's2member_level0' && $role !== 's2member_level1' )
        return;

    $user = new WP_User( $user_id );

    if ( $role === 's2member_level0' )
        $user->add_role( 'customer' );
    else
        $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.

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 ) {
    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.

Source Link
TheDeadMedic
  • 36.6k
  • 9
  • 68
  • 102

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 ) {
    if ( $role !== 's2member_level0' && $role !== 's2member_level1' )
        return;

    $user = new WP_User( $user_id );

    if ( $role === 's2member_level0' )
        $user->add_role( 'customer' );
    else
        $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.