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.

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
    Apr 7 '20 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. Apr 7 '20 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
    Apr 7 '20 at 15:26
  • @TomJNowell Any automated way of giving them a role on the main site? Apr 7 '20 at 15:30
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

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.

1
  • Thanks! I added it to functions.php and it seems that it did something because now I received the second email with username and password after clicking the confirmation link in the activation email (wasn't getting the second email before). But the user is still not added to the main site. Any idea why? Apr 7 '20 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.