Create multiple subdomains for multi-site from a database

More than an answer, a study on the matter.

The Excel issue


Relevant Q&A

What’s common to all is: no definitive procedure has been recorded in this Stack.


Testing code

  • Based on OneTrickPony first Question.
  • Must run only once. I don’t know of a better method.
  • I’m not sure if admin_init is the best place to run this.
  • Haven’t measured, but this code does not runs fast (3 sites in this sample).
  • There’s an awful CSS bug in the admin after running the code. After disabling the hook and reloading the page everything is ok and all sites had been created.

. (!?) enter image description here

/* Enable the hook, refresh the dashboard, <WAIT>, disable again */
// add_action('admin_init','wpse_54647_testing_code');

function wpse_54647_testing_code()
{
    $site = get_current_site();
    
    $meta = array( 
            'blogdescription'       => 'blog description'
        ,   'users_can_register'    => 1 
    );
    
    $sites_array = array(
        
            array(
                    'title'   => 'site 1'
                ,   'domain'  => 'site1'
                ,   'user'    => 'site1'
                ,   'pass'    => 'pass1'
                ,   'email'   => '[email protected]'
            )
            
        ,   array(
                    'title'   => 'site 2'
                ,   'domain'  => 'site2'
                ,   'user'    => 'site2'
                ,   'pass'    => 'pass2'
                ,   'email'   => '[email protected]'
            )
            
        ,   array(
                    'title'   => 'site 3'
                ,   'domain'  => 'site3'
                ,   'user'    => 'site3'
                ,   'pass'    => 'pass3'
                ,   'email'   => '[email protected]'
            )
    );
    
    for( $i = 0; $i < count($sites_array); $i++ )
    {
        $user = wp_insert_user( array (
                                    'user_login' => $sites_array[$i]['user']
                                ,   'user_pass' => $sites_array[$i]['pass'] 
                                ,   'user_email' => $sites_array[$i]['email'] 
                                ) );
        
        $blog_id = wpmu_create_blog(
                $sites_array[$i]['domain'] . '.' . $site->domain
            ,   "https://wordpress.stackexchange.com/"
            ,   $site_titles[$i]['title']
            ,   $user
            ,   $meta
            ,   $site->id
        );
    }

}

Leave a Comment