admin管理员组文章数量:1391937
I'm working on a multisite installation and I have this function to hook to a WordPress action.
The objective is to 'add' user roles to users on the sub-sites as the same user's role changes on the main site.
This function - using wp_update_user
- changes the desired user's role at sub-sites to the new role and removes other roles they have, but leaves the role on the main site as is:
function updateRole_onSubSites( $therole ){
$user_id = wp_get_current_user()->ID;
foreach ( get_sites() as $site ) {
switch_to_blog( $site->blog_id );
if ( !current_user_can('update_core') && !is_main_site() ){
$updateUser = wp_update_user(array(
'ID' => $user_id,
'role' => $therole
));
}
restore_current_blog();
}
}
What I need is to add roles to the existing role on the sub-site. So I tried the code below - using add_role
- which adds roles to the user only on the main site but does absolutely nothing on sub-sites, whereas it should be the other way round:
function updateRole_onSubSites( $therole ){
$user_id = wp_get_current_user()->ID;
$theuser = new WP_User( $user_id );
foreach ( get_sites() as $site ){
switch_to_blog( $site->blog_id );
if ( !current_user_can('update_core') && !is_main_site() ){
$theuser->add_role( $therole );
}
restore_current_blog();
}
}
Help please! Thank you.
Okay, so I finally discovered a simple solution, I only had to move the $user_id
and $theuser
variables to after the switch_to_blog
function:
function updateRole_onSubSites( $therole ){
foreach ( get_sites() as $site ) {
switch_to_blog( $site->blog_id );
if ( !current_user_can('update_core') && !is_main_site() ){
$user_id = wp_get_current_user()->ID;
$theuser = new WP_User( $user_id );
$theuser->add_role( $therole );
}
restore_current_blog();
}
}
Now it works perfectly as intended.
本文标签: multisitePreferred method of setting user role only works on Main Site in Network
版权声明:本文标题:multisite - Preferred method of setting user role only works on Main Site in Network 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744714797a2621338.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论