0

I have a multisite network set up with around 250 sites. I'd like to be able to copy users with their assigned role from the main site in the network to subsites. Here is how I attempted to do this:

functions.php

add_action('wp','add_current_user_to_site',10);

function add_current_user_to_site() {

    if(!is_user_logged_in()) {
        return false;
    }

    $current_user = wp_get_current_user();
    $blog_id = get_current_blog_id();
    $user_id = ($current_user->ID);

    switch_to_blog(1);
    $get_role = ($current_user->caps);
    $add_role = key($get_role);
    restore_current_blog();

    if (!is_user_member_of_blog( $user_id, $blog_id ) ) {
        add_user_to_blog( $blog_id, $user_id, $add_role );
    }

}

This has been tested and works when a user visits the subsite THEN proceeds to log in. If they attempt to log in directly, they receive an error.

Is this the best way to set up copying a user and adding them to subsites? Is there a way to allow it to happen when visiting the login directly, rather than needing to visit the site first?

  • Does my answer helped you ? Or do you have any other query ? @cul8r – CodeMascot Oct 24 '16 at 3:33
1

You can use the below function to copy all the users to all the subsite. It's actually getting all the user IDs and subsites IDs first, then it's looping over every sites and users to assign all the users to every subsite. Here is the updated code-

add_action('wp','the_dramatist_add_current_user_to_site');
function the_dramatist_add_current_user_to_site() {
    global $wpdb;
    $all_users = $wpdb->get_col( 'SELECT ID FROM $wpdb->users' );
    $subsites = get_sites();
    foreach( $subsites as $subsite ) {
        $subsite_id = get_object_vars($subsite)['blog_id'];
        foreach ( $all_users as $current_user) {
            $blog_id = get_current_blog_id();
            $user_id = ($current_user->ID);

            switch_to_blog($subsite_id);
            $get_role = ($current_user->caps);
            $add_role = key($get_role);
            restore_current_blog();

            if (!is_user_member_of_blog( $user_id, $blog_id ) ) {
                add_user_to_blog( $blog_id, $user_id, $add_role );
            }
        }
    }
}

Hope that thing helps.

0

Using the other answer provided, it appears there are two ways to copy users with their assigned roles from the main site to subsites in a WordPress multisite network.

It can be set up to either add the current user or all users from a site (I chose the main network site, blog_id=1) to the subsite when they visit the page.

Here is how to do both, choose one and add to functions.php:

Add only the user to each subsite

function add_current_user_to_site() {
    if(!is_user_logged_in()) {
        return false;
    }

    $current_user = wp_get_current_user();
    $blog_id = get_current_blog_id();
    $user_id = ($current_user->ID);

    switch_to_blog(1);
    $get_role = ($current_user->caps);
    $add_role = key($get_role);
    restore_current_blog();

    if (!is_user_member_of_blog( $user_id, $blog_id ) ) {
        add_user_to_blog( $blog_id, $user_id, $add_role );
    }
}
add_action('wp','add_current_user_to_site');

Add all users to each subsite

function add_all_users_to_site() {
    if(!is_user_logged_in()) {
        return false;
    }

    $users = get_users('blog_id=1');
    $sites = get_sites();

    foreach ($sites as $site) {
        foreach ($users as $user) {
            $blog_id = get_current_blog_id();
            $user_id = ($user->ID);

            switch_to_blog($site->id);
            $get_role = ($user->caps);
            $add_role = key($get_role);
            restore_current_blog();

            if (!is_user_member_of_blog( $user_id, $blog_id ) ) {
                add_user_to_blog( $blog_id, $user_id, $add_role );
            }
        }
    }
}
add_action('wp','add_all_users_to_site');
  • Rather than assume that the main site's blog ID will always be 1, you can use the Multisite constant BLOG_ID_CURRENT_SITE. – Pat J Oct 25 '16 at 14:45

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.