User Registration Across Sites within a Multisite Network

WordPress multisite installs come with many benefits. But they also come with some quirks. One such quirk is how it handles user registration on sibling sites within the multisite network.

Hypothetical Situation

In this hypothetical scenario, we will have Site A, Site B, and a user named Alice. Previously, Alice had registered as a user for Site A. Now, however, she would like to register for Site B, as well. This is where the odd WordPress behavior kicks in due to it being overly “helpful.”

In this situation, upon attempting to register and sign into Site B, WordPress will, instead, display a page to her saying that she does not have access to Site B and encourages her to switch over to Site A, as it knows she already has an account over there.

WordPress treats her registration request as an error—it assumes she attempted to sign into the wrong site. However, this is not the behavior we want. We want Alice to also register as a user with Site B.

[A] Solution

As with all things WordPress, there are multiple ways to achieve the same outcome. This post simply explores the route that we chose to take.

Step by Step:

To start, we must find a hook that allows us to take action when a user signs in. Fortunately, WordPress has a simple hook that allows us to do just that: wp_login. In this example, we will call our function “add_to_subsite” but you can name it whatever makes sense to you. Also note that, whatever you name your function, it will be receiving a user_login object (which you’ll see in a code snipet below).

				
					function add_to_subsite( $user_login ) {
    // Code will be written here
}
add_action( 'wp_login', 'add_to_subsite');
				
			

Now that we have the hook we need, it’s time to start coding the logic needed to modify WordPress’s default behavior. Before we go further, though, it’s unnecessary to add network admins as a user in each site so let’s ensure this code does not run for those users.

				
					// Don't bother if user is network admin
if( is_network_admin() ) {
    return;
}
				
			

The next step is to search the network’s list of users to see if the user attempting to sign in is already a user on the network. To do this, we will be using get_users. Note: On a multisite network, you will be checking against a blog_id of 0.

				
					// Search for user in the network
$args = array( 'blog_id' => 0, 'search' => $user_login );
$user = get_users( $args );
				
			

We only want/need to run this code if the user was found as an existing user on the network so we will be wrapping the next snippet within a basic if statement. From there, it’s a simple matter of collecting the blog_id of the site being signed into and the user’s user_id. Now is also a good time to declare what type of role you will want to be assigning to this user. Always defer to the lowest level of priviledge possible (in this case: subscriber).

				
					// If user was found
if( $user ) {

    // Vars
    $blog_id = get_current_blog_id();
    $user_id = get_current_user_id();
    $desired_role = 'subscriber';

}
				
			

Lastly, if the user is not an existing user of the current blog, we will add them using add_user_to_blog and pass it the user_id and the blog_id we obtained previously.

				
					// If not member of current site
if( !is_user_member_of_blog( $user_id, $blog_id ) ) {

    // Add user to site
    add_user_to_blog( $blog_id, $user_id, $desired_role );

}
				
			

All Together Now

That’s it! Below is all the code wrapped into one snippet.

				
					function add_to_subsite( $user_login ) {

    // Don't bother if user is network admin
    if( is_network_admin() ) {
        return;
    }

    // Search for user in the network
    $args = array( 'blog_id' => 0, 'search' => $user_login );
    $user = get_users( $args );
    
    // If user was found
    if( $user ) {
    
        // Vars
        $blog_id = get_current_blog_id();
        $user_id = get_current_user_id();
        $desired_role = 'subscriber';
        
        // If not member of current site
        if( !is_user_member_of_blog( $user_id, $blog_id ) ) {
        
            // Add user to site
            add_user_to_blog( $blog_id, $user_id, $desired_role );
        
        }
    
    }

}
add_action( 'wp_login', 'add_to_subsite' );