如何更改用户角色?
9 个回答
- 投票数
-
- 2010-12-01
请参见 WP_User类,您可以使用它来添加和删除以下角色用户.
编辑:最初我确实应该提供更多有关此答案的信息,所以我在下面添加了更多信息.
更具体地说,可以通过创建WP_user类的实例并调用
add_role()
或remove_role()
方法来设置用户角色.示例
将订阅者角色更改为编辑者
// NOTE: Of course change 3 to the appropriate user ID $u = new WP_User( 3 ); // Remove role $u->remove_role( 'subscriber' ); // Add role $u->add_role( 'editor' );
希望这比我最初的回答要有用,但不一定那么有用.
See the WP_User class, you can use this to add and remove roles for a user.
EDIT: I really should have provided more information with this answer initially, so i'm adding more information below.
More specifically, a user's role can be set by creating an instance of the WP_user class, and calling the
add_role()
orremove_role()
methods.Example
Change a subscribers role to editor
// NOTE: Of course change 3 to the appropriate user ID $u = new WP_User( 3 ); // Remove role $u->remove_role( 'subscriber' ); // Add role $u->add_role( 'editor' );
Hopefully that's more helpful than my initial response, which wasn't necessarily as helpful.
-
`remove_role()`和`add_rule()`将数据保存到数据库吗?`remove_role()` and `add_rule()` save data to the database?
- 0
- 2019-10-29
- b_dubb
-
是@b_dubb,两种方法都通过`update_user_meta()`方法[在这里](https://developer.wordpress.org/reference/functions/update_user_meta/)保存到db.参见`add_role()`[此处](https://developer.wordpress.org/reference/classes/wp_user/add_role/)和`remove_role()`[此处](https://developer.wordpress.org/reference/classes/wp_user/remove_role/)Yes @b_dubb, both methods save to db through the `update_user_meta()` method [here](https://developer.wordpress.org/reference/functions/update_user_meta/). See `add_role()` [here](https://developer.wordpress.org/reference/classes/wp_user/add_role/) and `remove_role()` [here](https://developer.wordpress.org/reference/classes/wp_user/remove_role/)
- 1
- 2020-01-07
- Gonçalo Figueiredo
-
那很方便.谢谢.That's pretty handy. Thanks.
- 0
- 2020-01-07
- b_dubb
-
set_role()将删除所有角色,并在一个命令中添加指定的角色`set_role()` will remove all roles and add the specified role in one command
- 0
- 2020-04-18
- G-J
-
- 2015-06-14
请注意,有一种更简单的方法来更改用户角色,这在您不知道用户的当前角色时特别有用:
->set_role()
示例:
// Fetch the WP_User object of our user. $u = new WP_User( 3 ); // Replace the current role with 'editor' role $u->set_role( 'editor' );
Just note that there is a simpler way to change the user role which is especially helpful when you do not know the current role of the user:
->set_role()
Example:
// Fetch the WP_User object of our user. $u = new WP_User( 3 ); // Replace the current role with 'editor' role $u->set_role( 'editor' );
-
请记住,set_role将删除用户的先前角色并分配新角色.Remember that set_role will remove the previous roles of the user and assign the new role.
- 0
- 2016-05-03
- shasi kanth
-
这非常适合自定义注册表格.首先注册没有角色的用户,然后在他们确认电子邮件时添加角色.This is perfect for custom registration forms. First register users with no roles and after that add role when they confirm email.
- 1
- 2017-09-15
- Ivijan Stefan Stipić
-
- 2012-10-29
如果要根据条件以编程方式进行操作,则可以在t31os的答案上进行推算,您可以在函数文件中添加类似的内容
$blogusers = get_users($blogID.'&role=student'); foreach ($blogusers as $user) { $thisYear = date('Y-7'); $gradYear = date(get_the_author_meta( 'graduation_year', $user->ID ).'-7'); if($gradYear < $thisYear) { $u = new WP_User( $user->ID ); // Remove role $u->remove_role( 'student' ); // Add role $u->add_role( 'adult' ); } }
To extrapolate on t31os's answer you can slap something like this in your functions file if you want to do this programmatically based on a condition
$blogusers = get_users($blogID.'&role=student'); foreach ($blogusers as $user) { $thisYear = date('Y-7'); $gradYear = date(get_the_author_meta( 'graduation_year', $user->ID ).'-7'); if($gradYear < $thisYear) { $u = new WP_User( $user->ID ); // Remove role $u->remove_role( 'student' ); // Add role $u->add_role( 'adult' ); } }
-
我认为您对$blogID的用法是错误的.[`get_users()`](http://codex.wordpress.org/Function_Reference/get_users)仍将默认使用当前博客ID.I think your usage of `$blogID` is wrong. [`get_users()`](http://codex.wordpress.org/Function_Reference/get_users) will use the current blog ID per default anyway.
- 0
- 2012-10-29
- fuxia
-
是的,在我的案例中,粘贴仅来自一个多站点示例.我也没有在这里定义它,因此显然会引发错误.yep, in my case the paste was just from a multisite example. I didn't define it here either so obviously it would throw an error.
- 0
- 2012-11-26
- Adam Munns
-
- 2015-04-16
您可以通过编辑用户个人资料来更改任何用户的角色.WordPress已内置此选项时,无需添加任何其他代码.
或
您可以使用代码将具有订阅者角色的所有当前用户更改为编辑者:
$current_user = wp_get_current_user(); // Remove role $current_user->remove_role( 'subscriber' ); // Add role $current_user->add_role( 'editor' );
You can change the role of any user by editing the users profile. No need to add any more code when this option is already built into WordPress.
Or
You could use code to change all current users with the subscriber role to editor:
$current_user = wp_get_current_user(); // Remove role $current_user->remove_role( 'subscriber' ); // Add role $current_user->add_role( 'editor' );
-
- 2010-12-01
有一个WordPress功能!
我认为最好在可用时使用WordPress功能.
您可以使用 wp_insert_user()函数,在该函数中需要使用其中一个参数提供的是$ userdata ['role'].在此参数中,您可以指定要将用户更改为的角色.
There's a WordPress function for that!
I think it is best to use WordPress functions, if and when they are available.
You can use the wp_insert_user() function, where one of the arguments that you will need to provide is the $userdata['role']. In this argument you can specify the role that you want to change the user into.
-
wp无法识别该功能.我收到"未定义函数"错误.wp doesn't recognize that function. I got an "undefined function" error.
- 0
- 2010-12-01
- Joann
-
从外观上看,wp_insert_user()似乎完全一样.提供ID时,该ID会更新.没有ID正在添加新用户.真的不知道wp_update_user()和wp_insert_user()之间有什么区别.By the looks of it, wp_insert_user() seems to do the exact same. When you provide an ID, that ID gets updated. No ID is adding new user. Don't really know what the difference between wp_update_user() and wp_insert_user() is, yet.
- 0
- 2010-12-01
- Coen Jacobs
-
-
- 2016-11-09
您可以使用 wp_update_user .您的代码应该像这样:
<?php $user_id = 3; $new_role = 'editor'; $result = wp_update_user(array('ID'=>$user_id, 'role'=>$new_role)); if ( is_wp_error( $result ) ) { // There was an error, probably that user doesn't exist. } else { // Success! } ?>
You can use wp_update_user. Your code shoud be like this:
<?php $user_id = 3; $new_role = 'editor'; $result = wp_update_user(array('ID'=>$user_id, 'role'=>$new_role)); if ( is_wp_error( $result ) ) { // There was an error, probably that user doesn't exist. } else { // Success! } ?>
-
- 2017-08-07
<?php $wuser_ID = get_current_user_id(); if ($wuser_ID) { // NOTE: Of course change 3 to the appropriate user ID $u = new WP_User( $wuser_ID ); // Remove role $u->remove_role( 'subscriber' ); // Add role $u->add_role( 'contributor' ); } ?>
<?php $wuser_ID = get_current_user_id(); if ($wuser_ID) { // NOTE: Of course change 3 to the appropriate user ID $u = new WP_User( $wuser_ID ); // Remove role $u->remove_role( 'subscriber' ); // Add role $u->add_role( 'contributor' ); } ?>
-
- 2019-03-27
我知道它的帖子很老,但是我发现用户的角色存储在
wp_usermeta
表中,键在meta_key
wp_capabilities >列.如果要更改用户角色,可以通过此简单功能完成.
函数change_role($id,$new_role){ GLOBAL $table_prefix; if(is_array($new_role)){ $new_role_array=$new_role; }其他{ $new_role_array=array($new_role=&gt;true); } 返回update_user_meta($id,$table_prefix.'capabilities',$new_role_array); }
有两种使用此功能的方法.
如果要更改单个角色的角色.
change_role(2,'editor');//编辑者是新角色
或者,如果要向用户添加多个角色,请在第二个参数中将角色用作数组.
$ roles_array=array('editor'=&gt;true,'administrator'=&gt;true);//角色数组 change_role(2,$ roles_array);
祝你好运.
I know its a very old Post, but i have found that the roles for users are stored in
wp_usermeta
table with keywp_capabilities
inmeta_key
column.If you want to change the user role you can do it by this simple function.
function change_role($id, $new_role){ GLOBAL $table_prefix; if(is_array($new_role)){ $new_role_array = $new_role; }else{ $new_role_array = array( $new_role => true ); } return update_user_meta($id, $table_prefix.'capabilities', $new_role_array); }
There is two way to use this function.
If you want to change the role for a single role.
change_role(2, 'editor'); // editor is the new role
Or if you want to add multi roles to the user, use the roles as array in the second parameter.
$roles_array = array('editor' => true, 'administrator' => true); // roles array change_role(2, $roles_array);
Good luck.
我在设置中具有自定义角色,并且希望能够通过功能自动更改用户的角色.假设用户A拥有SUBSCRIBER角色,如何将其更改为EDITOR?添加角色时,我们只是:
如何更改角色?是否有类似的东西:
更新: 我认为这可以做到: