Creating child site in a Multisite network through code in WordPress

| By Webner

Creating child site on user registration in a Multisite network through code in WordPress

Multisite Network –
Multisite is a WordPress feature which allows users to create a network of multiple sites on a single WordPress installation i.e in a single WordPress project.
And it is available since WordPress 3.0.

Multisite Network features –

  • We can run multiple WordPress blogs or websites from a single installation.
  • We can have a network of subdomains or directories i.e. or
  • As a super admin we can install themes and plugins in the parent site, which will be available for the child sites too.

Enable Multisite Network In WordPress –


STEP 1: Add some extra code in the wp-config.php file of parent site.

Add following code in your project’s wp-config.php file just before the
/* That’s all, stop editing! Happy blogging. */ line.
define( ‘WP_ALLOW_MULTISITE’, true );

This line simply enables the multisite feature on a WordPress site.

STEP 2: Setting up Your WordPress Multisite Network

For setting up your wordpress multisite network just go to admin dashboard of your site then navigate to Tools -> Network Setup.

Multisite network through code in WordPress

If you have any plugin installed in your wordpress site then it will ask to deactivate all before network settings whenever you open “Network Setup” after adding that line(step 1) in wp-config.php file.

You can see in screenshot below.

Multisite network through code in WordPress

So first you have to deactivate all installed plugins from the site and then try again to setup the network settings from Tools -> Network Setup.

After deactivate all the plugins from the site, you will see something like this under this tab

Click “Install” button after filling the appropriate information it is asking for.

Multisite network through code in WordPress

Follow the steps and copy code from here and paste it in the your wp-config.php and .htaccess files of your project.

Tip: .htaccess is a hidden file so you may not able to see it in your project directory, just use ctrl+H to see all hidden files.

After saving both the files just login again and you will see the multisite dashboard. Something like this:

Multisite network through code in WordPress

Create A Child Site In The Multisite Network

Now there are two methods to create a child site inside the network
(i) Using admin dashboard
(ii) Using code

We are going to create a child site through code on the registration of a new user using REST API.

Step 1: First we will add the create API endpoint/method using REST API in the function.php file, like this

  add_action( 'rest_api_init', function () {
        register_rest_route( userAPI, '/createUserSite, array(
            'methods' => 'POST',
            'callback' => ‘create_site_endpoint’,

Hence my REST API will be

Step 2: And we are using this API on user registration action, that is

add_action( “user_register”, “create_site_on_user_registration”, 10, 1 );
function create_site_on_user_registration( $user_id )
    $user  = new WP_User( $user_id );
    $user_name = stripslashes( $user->user_login );
    $domain =$_SERVER['HTTP_HOST'];
    $site_title= "Dealer ".$user_name ."'s site!";
    $url = “”;       
    /** use your site’s URL instead of **/

    $ch = curl_init($url);          /**Creating a CURL request**/
    $data = array(
 	   'subdomain' => $subdomain,
 	   ‘title’ => $site_title,
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);

So, whenever a new user will register himself, this method will be called and will further request the REST API, and finally the endpoint registered with this API will be called.
The username sent by this method will be used as the subdomain name of his site in the multisite network and the site title in the curl request.

Step 3: And the create site endpoint would be like this

function create_site_endpoint($request)
    global $wpdb,$table_prefix;
    $theme = wp_get_theme();
   $params = $request->get_params();
   $blogTable=$wpdb->prefix . 'blogs';
    if (array_key_exists ( "subdomain", $params ))
    } else {
        $siteResponse["error"] = "Sub Domain name is required";
        return $siteResponse;
    $blogs = $wpdb->get_results ( "SELECT * FROM $blogTable WHERE domain LIKE '$subdomain1.%'" );               
    if(count($blogs) != 0)  
   /** Check if any other site already exists with this domain name or not **/
        $siteStatus ["error"]='Site with this subdomain is already existing';
        return $siteStatus;
        $path = "/";
        $title =trim($params['title']);
        $n = wpmu_create_blog ($subdomain, $path, $title, 1 );
        if (!is_int($n))
            return 'Error while creating Site';
            $siteDetails["request_status"]= "Site Created Successfully";
            $siteDetails["user's site"]= $subdomain;
            $siteDetails["site title"]= $title;                 
            return $siteDetails;
/************ generate full child site URL module ********************/
function get_full_site_URL($subdomain)
    if($subdomain != null){
        $domain = $_SERVER ['HTTP_HOST'];
        $UrlParams = explode ( '.', $domain );
        if ($UrlParams [0] == 'www') {
            $newSubDomain = str_replace ( "www",$subdomain, $domain );
        } else {
            $newSubDomain = $subdomain . "." . $domain;
        return $newSubDomain;
        return $newSubDomain;

So, on user registration, the create_site_on_user_registration() will be invoked, and hence a request to
REST API will be sent which in turn, will call the create_site_endpoint() endpoint, where a child site inside the multisite network will be created for that user.

All this code should be included inside the function.php file of the project’s current theme.

Leave a Reply

Your email address will not be published. Required fields are marked *