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.
1 Answer
Reset to default 0You 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
hookis_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.
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