Programmatically Create New Sites In WordPress Multisite

In WordPress, creating new sites from the admin interface can be tedious, especially if you want to add custom metadata to sites/ACF option fields.

I had a scenario where I needed to create 1800 sites from a spreadsheet. Doing it one-by-one was not going to cut it, so I needed a code solution where I could loop through these sites and create them without needing the UI.

Like almost everything in WordPress, there is a function you can call. It’s called wpmu_create_blog — I have a Multisite subdomain install, so this code won’t work for directory-based multisites (it’s not hard to change, though). And I found the documentation to be quite poor. But, here is what I ended up doing.

// The mysubdomain is the part that determines the subdomain of your site
// maindomain.com would be your primary domain your subdomains are using
$domain = "mysubdomain.maindomain.com";

// Keep this
$path = "/";

// Name of your site
$title = "My Awesome Site";

// User ID of the owner of the site
// User ID 1 is the first account on a WordPress install
// You can either create a new user or use an existing ID
$userId = 1;

wpmu\_create\_blog($domain, $path, $title, $userId);

While I did not include the loop in this example, this is what I would be calling inside of the loop.

The wpmu_create_blog function returns the ID of the newly created site on success or a WordPress error object on error.

I would recommend doing something like this:

$result = wpmu\_create\_blog($domain, $path, $title, $userId);

if ( !is\_wp\_error($result) ) {
  	// The message, "Site created! ID"
    echo "Site created! " . $result;
} else {
  	// It's a WordPress error object
    print\_r($result);
}

That’s all there is to it.