admin管理员组文章数量:1193364
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.
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.
Share Improve this question edited Oct 6, 2022 at 13:48 Galloway Grumblefield asked Oct 5, 2022 at 22:04 Galloway GrumblefieldGalloway Grumblefield 351 silver badge8 bronze badges1 Answer
Reset to default 2It 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.
本文标签: user rolesRemoverole not workinggives quotCall to undefined method stdClassremoverole()quot
版权声明:本文标题:user roles - Remove_role not working, gives "Call to undefined method stdClass::remove_role()" 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738394467a2084428.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论