WordPress

Multiple Users With WordPress Multisite

When you are using the WordPress multisite it's very easy to create a new site and straight away start using it just like your other WordPress sites.

In WordPress when you create a new site you are asked for the email address of the admin user for the site, if this email address is already in the user database then this user is added as the admin for the site.

If the user doesn't exist then a new user is created and added to this new site as the site admin. This is fine if you only have one author/editor per site, but what if you want the authors from the other sites to use this new site the same way?

You will have to manually give them access to the new site, if you have a lot of users or often create new sites this can take up a lot of time. The following code examples will show you that when a new site is added we can loop through all users on the other sites and add them to this site. We can then go the other way and make it so when a user is created we automatically give them access to all the other sites within the network.

Add All Users To New Sites

To add all users to a new site we need to use the action wpmu_new_blog, which will run after the new site has been created, this will run our function and pass in the new blog ID.

add_action( 'wpmu_new_blog', 'add_all_users_to_new_site' );

Inside this function we need to get a list of all sites on the network, so we can get all the users assigned to these sites.

We used to be able to get a list of all blogs by using the function get_blog_list() but this was deprecated in version 3.0. Therefore we need to use a custom query on the $wpdb object to get a list of all sites inside the blogs table.

When we have all the sites we can then loop through this information and get a list of all the users on this site by using the get_users() function.

As a user can have multiple roles we need to loop through all these roles and add the user to the site with this specific role by using the function add_user_to_blog().

/**
 * Create a new site
 * Loop through all users and add them to the new blog
 *
 * @param  INT $blog_id - New blog ID
 *
 * @return void
 */
function add_all_users_to_new_site($blog_id)
{
    global $wpdb;

    // Query all blogs from multi-site install
    $blogs = $wpdb->get_results("SELECT blog_id,domain,path FROM ".$wpdb->base_prefix."blogs");

    foreach($blogs as $blog)
    {
        $users = get_users( array('blog_id' => $blog->blog_id) );

        if(!empty($users))
        {
            foreach($users as $user)
            {
                if(!empty($user->roles))
                {
                    foreach($user->roles as $role)
                    {
                        add_user_to_blog($blog_id, $user->ID, $role );
                    }
                } else {
                    add_user_to_blog($blog_id, $user->ID, 'subscriber' );
                }
            }
        }
    }
}

Add New Users To All Sites

After we can create a new site and all users are added to it, we need to have the same ability when adding a new user. If you have 100s of sites you wouldn't want to go through all these sites to add the new user on to it, so this function will loop through all the sites and add the user to them.

To start off we need to use the action user_register which will run after a new user has been created, this will run our function and pass in the new user ID.

add_action( 'user_register', 'add_new_user_to_all_sites' );

This function will need to do the same as the query above and get all the sites from the blogs table in the database. We then need to get the user object by passing in the $user_id into the WP_User, from here we will have access to the user roles.

Now we can loop through all the sites and add the user to the site by using the add_user_to_blog() function.

/**
 * Add a new user to all other sites
 *
 * @param  INT $user_id - New User ID
 *
 * @return void
 */
function add_new_user_to_all_sites( $user_id )
{
    global $wpdb;

    // Query all blogs from multi-site install
    $blogs = $wpdb->get_results("SELECT blog_id,domain,path FROM ".$wpdb->base_prefix."blogs");
    $user = new WP_User( $user_id );

    if(!empty($blogs))
    {
        foreach($blogs as $blog)
        {
            foreach($user->roles as $role)
            {
                add_user_to_blog($blog->blog_id, $user_id, $role );
            }
        }
    }

    return true;
}
Back to top