0

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()

UPDATE

With some more detailed development, I got it to work. I don't know if there was any hope for the WordPress remove_role and add_role, but I found an example using the UM class. This requires Ultimate Member, of course, so it's not a "pure" WP solution. Does work well, though.

This code works:

function changeRole($user,$this_role,$user_roles){
if( ! in_array("administrator",$user_roles) ){
    if( ! in_array($this_role,$user_roles) || ( count($user_roles) > 1 ) ){
        // If this user currently does not have the correct role, set it here.
        //$user->set_role( $this_role ); // OLD
        $new_roles_array = array();
        foreach($user_roles as $role){
            if($role !== $this_role){
                UM()->roles()->remove_role( $user->ID, $role ); // NEW
                $new_roles_array = array_diff($user_roles, [$role]);
            }
        }
        if( count($new_roles_array) == 0 || isset($new_roles_array[0]) && $new_roles_array[0] !== $this_role ){
            UM()->roles()->set_role( $user->ID, $this_role ); // NEW
        }
    }
}

}

In retrospect, I accepted @bob as the correct answer because I realize that when I performed the get_users operation, I did not set all fields for the user object:

$users = get_users( array( 'fields' => array( 'ID','user_login' ) ) );

It's possible this was the problem, where an object with minimal proprties was passed to the changeRole function.

1 Answer 1

2

It looks like your $user variable contents are not an instance of a WP_User class. You've got an error of stdClass, but you need the WP_User. Check what class the $user is the instance.

If you use the get_users() function, you should note one feature of this function described in the documentation:

Return value is an array of IDs, stdClass objects, or WP_User objects, depending on the value of the ‘fields‘ parameter.

  • If ‘fields‘ is set to ‘all’ (default), or ‘all_with_meta’, it will return an array of WP_User objects.
  • If ‘fields‘ is set to an array of wp_users table fields, it will return an array of stdClass objects with only those fields.
  • If ‘fields‘ is set to any individual wp_users table field, an array of IDs will be returned.
2
  • I had to do a considerable amount of tweaking and development. Ultimately, I ditched the WP set_role and remove_role in favor of the Ultimate Member UM() class, which works well. Also, my code was suffering from logic errors, which helped when corrected0 . Solution code added to original post. Commented Oct 6, 2022 at 10:15
  • I explained why I accepted your answer in the UPDATE section of my post. Commented Oct 6, 2022 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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