0

I have a multisite install with 2 sites. I am working on site A, and I have a lot fo users on site B.

I can individually move users from B to A, but there is a nontrivial number of users, and this could take days of repetitive work to do.

How would I move assign all of the users at once to site A from site B?

2 Answers 2

4

Here is code I wrote to automatically move the users from site A to site B within a network en masse. May need running twice

<?php

add_action( 'admin_init', 'user_move_init' );
add_action( 'admin_menu', 'user_move_add_page' );

/**
 * Init plugin options to white list our options
 */
function user_move_init(){
    register_setting( 'user_move', 'user_move', 'user_move_validate' );
}

/**
 * Load up the menu page
 */
function user_move_add_page() {
    add_users_page( __( 'User Import', 'user_move' ), __( 'User Import', 'user_move' ), 'edit_theme_options', 'theme_options', 'user_move_do_page' );
}

/**
 * Create the options page
 */
function user_move_do_page() {

    if ( ! isset( $_REQUEST['settings-updated'] ) )
        $_REQUEST['settings-updated'] = false;

    ?>

    <div class="wrap">
        <h2><?php echo __( ' User Network Import', 'user_move' ) ; ?></h2>

        <?php if ( false !== $_REQUEST['settings-updated'] ) : ?>
            <p>Starting transfer</p><?php
            global $wpdb;
            switch_to_blog(2);

            $aUsersID = $wpdb->get_col( $wpdb->prepare("SELECT $wpdb->users.ID FROM $wpdb->users" ));
            /*
            Once we have the IDs we loop through them with a Foreach statement.
            */
            foreach ( $aUsersID as $iUserID ) :
                /*
                We use get_userdata() function with each ID.
                */
                $user = get_userdata( $iUserID );

                $capabilities = $user->{$wpdb->prefix . 'capabilities'};
                if(!empty($capabilities)){

                    if ( !isset( $wp_roles ) ){
                        $wp_roles = new WP_Roles();
                    }
                    $r = '';
                    foreach ( $wp_roles->role_names as $role => $name ) :
                        if ( array_key_exists( $role, $capabilities ) ){
                            $r = $role;
                            break;
                        }

                    endforeach;
                }
                if(empty($r)){
                    $r = 'subscriber';
                }

                /*
                Here we finally print the details wanted.
                Check the description of the database tables linked above to see
                all the fields you can retrieve.
                To echo a property simply call it with $user->name_of_the_column.
                In this example I print the first and last name.
                */
                echo '<li>Transfering '.$user->user_login.' ('. ucwords( strtolower( $user->first_name . ' ' . $user->last_name ) ) . '), '.$r.'</li>';

                add_user_to_blog(1,$iUserID,$r);

                /*
                 The strtolower and ucwords part is to be sure
                 the full names will all be capitalized.
                */
            endforeach;

            restore_current_blog();

            ?><p>Transfer complete</p>

        <?php endif; ?>

        <form method="post" action="options.php">
            <?php settings_fields( 'user_move' ); ?>
            <p>Click to import Users from the blog site to the main site</p>
            <p class="submit">
                <input type="submit" class="button-primary" value="<?php _e( 'Start Import', 'user_move' ); ?>" />
            </p>
        </form>
    </div>
    <?php
}

/**
 * Sanitize and validate input. Accepts an array, return a sanitized array.
 */
function user_move_validate( $input ) {
    return $input;
}
5
  • It should be pointed out that this is a handy example, but it really needs to be modified to be useful for anybody else. Including site ids and it looks like it will move ALL users in the network into the specified site (id: 1).
    – Jake
    Commented Dec 28, 2015 at 20:06
  • Also, it won't move custom meta data, which is likely a big drawback.
    – Jake
    Commented Dec 28, 2015 at 20:12
  • Scratch that last comment. That was completely dumb. Of course it will add all info, it is just creating an association between an existing user and a new blog on the multisite. This is a really good framework for manually making this type of move. You can make a user query to get a specific blog though instead of all users: $wp_user_query = new WP_User_Query( array( 'blog_id' => 180, 'role' => 'Subscriber', ) );
    – Jake
    Commented Dec 28, 2015 at 20:54
  • this is great thanks, just to add, this doesn't move them, but I guess copies them (as in it creates an association with the new blog ID, and keeps the existing association in tact too)
    – rmorse
    Commented May 18, 2018 at 12:41
  • 1
    Indeed, users live in the user table, not site A, or site B, so copying isn't even accurate, this code attaches them to Site B, it doesn't detach them from site A though, remove_user_from_blog would do that, and it can be done after/before the add_user_to_blog call
    – Tom J Nowell
    Commented May 18, 2018 at 12:46
0

This blog post shows you how you can move users from one site into a blog that exists on a multisite. With a bit of modification you can get this to work from multisite blog to multisite blog.

2
  • While I'm not a fan of fiddling with SQL ( was hoping that the Wordpress APIs would be able to do the job ), this seems close enough, I'll mark it as the answer once I've had a try later today
    – Tom J Nowell
    Commented Aug 11, 2011 at 11:49
  • Nope this isnt what I wanted. This is moving from 1 multisite to another multisite. I only have 1 single multisite install. I had a lot of users assigned to one site but not another in the network. The database tables are not setup for individual user tables for each site in a network, so no modification of this tutorial will suffice
    – Tom J Nowell
    Commented Aug 13, 2011 at 23:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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