Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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;
}
share|improve this answer
add comment

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.

share|improve this answer
    
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 Aug 11 '11 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 Aug 13 '11 at 23:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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