admin管理员组文章数量:1327661
I have issues when trying to update or set new user role from an array returned by external API :
// array of roles
$roles = array("student", "pro" .... );
// get user data
$user = get_user_by("id", 35);
// Append new roles to this user
foreach($roles as $role){
$new_role = add_role( strtolower($role), $role, []);
$user->set_role($role);
}
When i check user details after:
print_r($user);
WP_User Object
(
[data] => stdClass Object
(
[ID] => 35
[user_login] => myuser
[user_pass] => $P$BoITs3rmyN5w47.gPi9hwzs2TqFhse1
[user_nicename] => myuser
[user_email] =>
[user_url] =>
[user_registered] => 2020-07-29 11:33:59
[user_activation_key] =>
[user_status] => 0
[display_name] => myuser
[user_level] => 0
)
[ID] => 35
[caps] => Array
(
[student] => 1
[pro] => 1
)
[cap_key] => yyOIY_capabilities
[roles] => Array
(
)
[allcaps] => Array
(
[student] => 1
[pro] => 1
)
[filter] =>
[site_id:WP_User:private] => 1
)
So, why add_role(...) adds caps rather than roles?
Thank you
I have issues when trying to update or set new user role from an array returned by external API :
// array of roles
$roles = array("student", "pro" .... );
// get user data
$user = get_user_by("id", 35);
// Append new roles to this user
foreach($roles as $role){
$new_role = add_role( strtolower($role), $role, []);
$user->set_role($role);
}
When i check user details after:
print_r($user);
WP_User Object
(
[data] => stdClass Object
(
[ID] => 35
[user_login] => myuser
[user_pass] => $P$BoITs3rmyN5w47.gPi9hwzs2TqFhse1
[user_nicename] => myuser
[user_email] =>
[user_url] =>
[user_registered] => 2020-07-29 11:33:59
[user_activation_key] =>
[user_status] => 0
[display_name] => myuser
[user_level] => 0
)
[ID] => 35
[caps] => Array
(
[student] => 1
[pro] => 1
)
[cap_key] => yyOIY_capabilities
[roles] => Array
(
)
[allcaps] => Array
(
[student] => 1
[pro] => 1
)
[filter] =>
[site_id:WP_User:private] => 1
)
So, why add_role(...) adds caps rather than roles?
Thank you
Share Improve this question edited Jul 29, 2020 at 13:58 Sandy asked Jul 29, 2020 at 12:39 SandySandy 32 bronze badges1 Answer
Reset to default 0Its better to register this role with add_role
( docs ) before adding it to user
Also, if you want to change roles attribute, try set_role
instead of add_role
本文标签: pluginsUpdate wordpress user roles not working as excepted
版权声明:本文标题:plugins - Update wordpress user roles not working as excepted 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742223434a2435597.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论