WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

  while($blog_count > 0){

    $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}";


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

  • $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?

share|improve this question
might have something to do with this, but that patch didn't work for me... – onetrickpony Sep 11 '11 at 22:22
The ticket got some more traction, just FYI. It's said the issue has been fixed. – hakre Oct 12 '11 at 23:08

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>';
    //echo '</pre>';


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 :)

share|improve this answer

Your Answer


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.