Creating multiple blogs

I’m trying to bulk-create a certain number of blogs:

  $blog_count = 5;
  $site = get_current_site();

  while($blog_count > 0){
    shuffle($words);

    $blog_title = implode(' ', array_slice($words, 0, mt_rand(1, 3)));
    $blog_domain = $blog_path = sanitize_key($blog_title);

    if(domain_exists($site->domain, $site->path.$blog_path, $site->id)) continue;

    $blog_id = wpmu_create_blog($site->domain, $site->path.$blog_path, $blog_title, 0, '', $site->id);

    echo "Created - {$blog_id}";
    $blog_count--;

  }

And I cannot get the paths set up correctly on all blogs. In this case only 2 out of 5 blogs work correctly.

Read More
  • $site-domain is “localhost”
  • $site->path.$blog_path is “/wpmutest/thenameoftheblog/”

(this is not a sub-domain install)

When I go and edit them, the “Update siteurl and home as well.” option is unchecked on the problematic blogs, and I cannot update it (it doesn’t change after I press save). It seems that the database tables are not getting created.

Am I doing something wrong here, or is this a bug?

Related posts

Leave a Reply

1 comment

  1. Apparently a global variable is not properly updated. I found a fix based on blackriver’s reply here, it’s a little ugly but it works:

    add_action('switch_blog', 'fix_wpmu_create_blog', 10, 2);
    function fix_wpmu_create_blog($new_blog_id, $old_blog_id){
      global $wpdb, $wp_queries;
    
      if($wp_queries && defined('FIX_WPMU_CREATE_BLOG')){
        $old_pattern = $wpdb->base_prefix.$old_blog_id.'_';
        $new_pattern = $wpdb->base_prefix.$new_blog_id.'_';
        $wp_queries = str_replace($old_pattern, $new_pattern, $wp_queries);
        //echo '<pre>';
        //print_r($wp_queries);
        //echo '</pre>';
      }
    }
    

    and

    define('FIX_WPMU_CREATE_BLOG', true);
    

    somewhere in the code that’s using this function.

    If someone has a better solution please post it so I can accept an answer 🙂