Question

I have a WordPress Multisite network. I'd like to have my main site on a seperate domain to my subdomains.

What I mean by this is, for example, mymainsite.com on the main site, but my subdomains are based off another domain, subdomains.com, such as example1.subdomains.com, example2.subdomains.com, and so on.

I've read about domain mapping, and I know that you can make subdomains then change Site URL to match, but I require that WordPress does this automatically so that when users sign up and register sites, it creates it under subdomains.com automatically.

Is this possible within one WordPress installation, or will I need to do two seperate installations?

Was it helpful?

Solution

Let's establish the baseline: it is possible to have the main site in example-domain-x.com and all subsites subdomains in example-domain-y.com. You just add subdomain1.example-domain-y.com (or subdomain2.example-domain-y.com, subdomain3.example-domain-y.com) like you would be adding a bare or www domain in the domain mapping panel ( URL containing wp-admin/network/settings.php?page=dm_domains_admin ).

annotated domain mapping screen in a WordPress Multisite

To achieve the same programmatically, you could hook a function to wp_normalize_site_data filter and change the domain in the data array.

add_filter('wp_normalize_site_data','wpse38196_wp_modify_domain_new_site');

function wpse38196_wp_modify_domain_new_site($data){
    // $data is an array, the same from wp_insert_site. See https://developer.wordpress.org/reference/functions/wp_insert_site/
    $original_domain = $data['domain'];x§   
    $data['domain'] = str_replace('mymainsite.com', 'subdomains.com', $data['domain']);
    return $data;
}

I presume you are aware, but in any case, you will also need to take care of setting up a new domain and SSL certificates in your HTTP server (i.e. apache, nginx), any reverse proxy (e.g. NGINX, Varnish, Cloudflare) and DNS server. This could be more harder to do proprogrammatically, including because the steps for doing change according to the setup.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top