user roles – Remove_role not working, gives “Call to undefined method stdClass::remove_role()”

Question

I’m making a special child theme template I’m going to use to match custom roles to a user’s subscription. So, this is going to be for admin-only maintenance use.

I made a function where I pass the user object, the new role (determined elsewhere), and the present role(s) for that user.

On this site, we require that each user have only one role.

Here is my “role removal” function.
variables:
$user is the user object for one user.
$this_role is the new role I want to assign the user.
$user_roles is the array of all roles for that user.

/*
* This function sets the current user to the role specified, and removes all other roles.
* It is true that multiple roles are allowed, but for the purposes of this website, multiple roles
* is not a desirable thing.
*/
function changeRole($user,$this_role,$user_roles){      
    if(! in_array($this_role,$user_roles) && ! in_array("administrator",$user_roles)){
        // If this user currently does not have the correct role, set it here.
        $user->set_role( $this_role );
    }
    foreach($user_roles as $role){
        if($role !== $this_role){
            // Delete all incorrect roles here.
            $user->remove_role( $role ); // <- Gives me an error
        }
    }
}

The error I’m getting is Uncaught Error: Call to undefined method stdClass::remove_role()

0
Galloway Grumblefield 2 months 2022-10-05T17:04:43-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse