5

I want to create multiple subdomains for users.
I know how to create multiple subdomains manually using creating network.

But, i have so many users, I can't create those subdomains manually one by one.

I have a user list in an Excel file:

id || username || password || subdomain

 1 || alex     || ****     || alex.websitewordpress.com

 2 || brad     || ****     || brad.websitewordpress.com

 3 || carol    || ****     || carol.websitewordpress.com

 4 || donald   || ****     || donald .websitewordpress.com

How can I create multiple subdomains automatically using the data from my Excel file?

2

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'   => 'user1@email.com'
            )

        ,   array(
                    'title'   => 'site 2'
                ,   'domain'  => 'site2'
                ,   'user'    => 'site2'
                ,   'pass'    => 'pass2'
                ,   'email'   => 'user2@email.com'
            )

        ,   array(
                    'title'   => 'site 3'
                ,   'domain'  => 'site3'
                ,   'user'    => 'site3'
                ,   'pass'    => 'pass3'
                ,   'email'   => 'user3@email.com'
            )
    );

    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
            ,   '/'
            ,   $site_titles[$i]['title']
            ,   $user
            ,   $meta
            ,   $site->id
        );
    }

}

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.