2

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?

1 Answer 1

3

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.

4
  • Does the main domain need to be mymainsite.com or does it need to be subdomains.com? By main domain, I mean the first domain labelled 'Main' when you go to the Sites page on Network Admin.
    – Maytha8
    Jan 31, 2021 at 4:35
  • The general idea and purpose of this solutions is that you can programmatically change the domain of the subsites to whatever you want, just like you can do it manually. Let me know if you need any further clarification. Jan 31, 2021 at 14:31
  • @AddesignSpace just edited my answer to clarify. Jan 31, 2021 at 14:48
  • Is it possible to also change the domain it shows on the admin page? Currently it shows .mymainsite.com. Can it be changed to .subdomains.com programmatically to make it a bit less misleading?
    – Maytha8
    Jan 31, 2021 at 23:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.