• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

I Like Kill Nerds

The blog of Australian Front End / Aurelia Javascript Developer & brewing aficionado Dwayne Charrington // Aurelia.io Core Team member.

  • Home
  • Aurelia 2
  • Aurelia 1
  • About
  • Aurelia 2 Consulting/Freelance Work

Programmatically Create New Sites In WordPress Multisite

Wordpress · July 25, 2022

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.

Dwayne

Leave a ReplyCancel reply

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
e:artisan
e:artisan
7 days ago

Just wanted to thanks for these snippets. For some reason I was struggling to add a new site to my multisite set up as it kept giving me the link expired error. These snippets helped me to add a new site within minutes.

0

Primary Sidebar

Popular

  • Handling Errors with the Fetch API
  • How To Get The Hash of A File In Node.js
  • How To Paginate An Array In Javascript
  • Thoughts on the Flipper Zero
  • Testing Event Listeners In Jest (Without Using A Library)

Copyright © 2023 · Dwayne Charrington · Log in

wpDiscuz