最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Automatic registration on main site upon user registration on Multisite

programmeradmin2浏览0评论

I have a WordPress multisite with a main site on it. From the multisite settings, registration of users is enabled.

When a user registers and confirms through email, they are added to the multisite but not to the main site. They have to be manually added to the main site.

How can I make it so that users who register on the multisite get automatically added to the main site also?

Technical details: It's a subdomain-based multisite, running WordPress version 5.3. The default user role in the main site settings is set to subscriber as defined in Network Admin => Sites => main site settings.

I have a WordPress multisite with a main site on it. From the multisite settings, registration of users is enabled.

When a user registers and confirms through email, they are added to the multisite but not to the main site. They have to be manually added to the main site.

How can I make it so that users who register on the multisite get automatically added to the main site also?

Technical details: It's a subdomain-based multisite, running WordPress version 5.3. The default user role in the main site settings is set to subscriber as defined in Network Admin => Sites => main site settings.

Share Improve this question edited Apr 7, 2020 at 15:23 Borislav Zlatanov asked Apr 7, 2020 at 15:02 Borislav ZlatanovBorislav Zlatanov 2611 gold badge2 silver badges8 bronze badges 4
  • Note that specifically, the users do not have a role on the main site, that's the critical piece of information you need to be able to solve this – Tom J Nowell Commented Apr 7, 2020 at 15:17
  • @TomJNowell The default role on the main site is set to subscriber - just updated the question with that information. Let me know if anything comes to mind in the way of solution. – Borislav Zlatanov Commented Apr 7, 2020 at 15:24
  • The users just need to be given a role on that site, the default role on the main site is just the role users who sign up on that specific site are given – Tom J Nowell Commented Apr 7, 2020 at 15:26
  • @TomJNowell Any automated way of giving them a role on the main site? – Borislav Zlatanov Commented Apr 7, 2020 at 15:30
Add a comment  | 

1 Answer 1

Reset to default 0

You can hook to wpmu_activate_user and add the user's role on the main site.

add_action( 'wpmu_activate_user', 'wpse363445_add_user_to_main_site' );
function wpse363445_add_user_to_main_site( $user_id ) {
    if ( is_main_site() ) {
        // Don't run if we're already on the main site.
        return;
    }
    if ( is_user_member_of_blog( get_main_site_id(), $user_id ) ) {
        // Don't add the user to the main site if they're already a member.
        return;
    }
    add_user_to_blog( get_main_site_id(), $user_id, 'subscriber' );
}

References

  • wpmu_activate_user hook
  • is_main_site()
  • add_user_to_blog()
  • get_main_site_id()
  • is_user_member_of_blog()

Note: This will only work for users that get sent an activation email. If you manually add a user to a site, and opt to not send them an activation email, you'll need to add them to the main site manually.

发布评论

评论列表(0)

  1. 暂无评论