• 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 Reply Cancel reply

0 Comments
Inline Feedbacks
View all comments

Primary Sidebar

Popular

  • I Joined Truth Social Using a VPN and Editing Some HTML to Bypass the Phone Verification
  • Testing Event Listeners In Jest (Without Using A Library)
  • How To Get The Hash of A File In Node.js
  • Thoughts on the Flipper Zero
  • Waiting for an Element to Exist With JavaScript
  • How To Paginate An Array In Javascript
  • How To Mock uuid In Jest
  • How To Decompile And Compile Android APK's On A Mac Using Apktool
  • How To Get Last 4 Digits of A Credit Card Number in Javascript
  • Wild Natural Deodorant Review

Recent Comments

  • CJ on Microsoft Modern Wireless Headset Review
  • Dwayne on Microsoft Modern Wireless Headset Review
  • CJ on Microsoft Modern Wireless Headset Review
  • john on Microsoft Modern Wireless Headset Review
  • Dwayne on Why You Should Be Using globalThis Instead of Window In Your Javascript Code

Copyright © 2023 · Dwayne Charrington · Log in

wpDiscuz