尝试从外部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);
}
当我在以下时间后检查用户详细信息时:
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
()
那么,为什么要添加\\u角色(…)添加大写而不是角色?
非常感谢。
最合适的回答,由SO网友:Az Rieil 整理而成
最好向注册此角色add_role
()docs ) 将其添加到用户之前
此外,如果要更改角色属性,请尝试set_role
而不是add_role