I have a subfolder install of WP multisite, let's say at "domain.com".

I now need to load the WP environment in subdomains of domain.com, say "sub1.domain.com", "sub2.domain.com", ... "subN.domain.com". Note that these subdomains do not correspond to WP blogs. But I do need to have access to the logged in user, the database, etc.

I have set up wildcard subdomains to load a php file that will display what I need for any particular subdomain, and I am including "wp-load.php" early in that file. The problem is that near line 99 in "ms-settings.php" it redirects to the main page of the site because $_SERVER[ 'HTTP_HOST'] is the subdomain, not the main site domain.

So how can I load the WP environment correctly in a non-blog subdomain?

I have a prototype that works, but I'm worried about the side effects of what I'm doing so it would be great if someone who's familiar with the core could weigh in.

What I am doing is pre-populating the $current_site and $current_blog globals appropriately before including "wp-load". Then, "ms-settings" doesn't try to create these and doesn't hit the code path that detects the subdomain and redirects to the front page.

I can now access member information (e.g. using 'get_userdata') and $wpdb.

Does this seem like a reasonable approach?

  • Trying to think of a more clear wording for this question without it getting too lengthy. Maybe "How to load WP environment in subdomain without Multisite taking over?" Mar 3, 2011 at 15:12
  • Sure, if you prefer short to accurate. :) I should probably add "multisite" though.
    – Greg
    Mar 4, 2011 at 4:15

4 Answers 4


Use the defines to make it pick the site you want it to pick.

You can define these four to setup the $current_site values properly: DOMAIN_CURRENT_SITE, PATH_CURRENT_SITE, SITE_ID_CURRENT_SITE, BLOG_ID_CURRENT_SITE.

If you check the wpmu_current_site() function in ms-load.php, you'll see that it uses those to create the $current_site global.

You may or may not have to populate the $current_blog global manually. Not sure. Try it and see.

So realistically, all you have to do is add something like this before you call wp-load.php:

define( 'DOMAIN_CURRENT_SITE', 'example.com' );
define( 'PATH_CURRENT_SITE', '/' );
define( 'SITE_ID_CURRENT_SITE', 1 );
define( 'BLOG_ID_CURRENT_SITE', 1 );

With the right values for your main example.com site, of course.

If you don't want to put these in the sub-domain's php files themselves, then you can do something like this in either the wp-config.php or the sunrise.php file (if you define SUNRISE to true in the wp-config.php as well, of course).

if ( $_SERVER['HTTP_HOST'] == 'sub1.example.com' || 
     $_SERVER['HTTP_HOST'] == 'sub2.example.com') {
    define( 'DOMAIN_CURRENT_SITE', 'example.com' );
    define( 'PATH_CURRENT_SITE', '/' );
    define( 'SITE_ID_CURRENT_SITE', 1 );
    define( 'BLOG_ID_CURRENT_SITE', 1 );

That's pretty much what the sunrise file load is there for, to allow a place where you can manually override things like this. Advantage of using sunrise.php over wp-config.php for it (which also works) is that you can easily turn sunrise on and off somewhere else, for testing and debugging and such.

  • Thanks Otto. This looks like exactly what I need. I'll give it a try and report back here.
    – Greg
    Mar 4, 2011 at 4:13

You could give a try to Wordpress Mu Domain Mapping plugin.


Actually, it better fits on your request to include wp-blog-header.php in the top of your php file of your subdomain, so you can load also the template functions.

    include(dirname(__FILE__) . "/../path_to_my_blog/wp-blog-header.php");

Or you can do a wp_redirect after the include of wp-load.php

  • 1
    @konus Maybe give on example of how he could use the plugin ...
    – EAMann
    Feb 23, 2011 at 5:41
  • @EAMAnn Actually, the plugin load the main blog completely in a subdomain. Reading twice, I understand that the question want to avoid that. Sorry for that :-/
    – konus
    Feb 23, 2011 at 16:23
  • @konus, Thanks for the answer, but it doesn't really address the question I am asking. I don't need the template functions so wp-load is fine in that respect. Also, the wp_redirect doesn't address the problem I am having. ms-settings is redirecting before it even gets to "after wp-load.php". Like I say in the question, one solution is to pre-populate the $current_site and $current_blog globals and I would like to know whether that might have any adverse side effects.
    – Greg
    Feb 25, 2011 at 4:59
  • @greg, You say "I do need to have access to the logged in user, the database, etc.". By including wp-blog-header.php you will able to do everything you want (instantiate $wpdb, load specifics wp- functions, etc. and also template functions). You could also use switch_to_blog when you want specific info from one blog in particular.
    – konus
    Feb 25, 2011 at 14:48
  • 1
    @konus, I do understand this. But I get what I need using wp-load. That is not the issue. Regardless of whether I user wp-load or wp-blog-header I will have the same problem of a redirect because the subdomain != primary domain. I guess what I'm saying is that your answer, while perhaps another interesting thing to debate, is not relevant to my question about ms-settings, which is loaded regardless. Or am I missing something? Are you saying that wp-blog-header somehow does solve the specific problem I am talking about.
    – Greg
    Feb 25, 2011 at 19:40

I was attempting the same thing as asked in this question and could not get around the redirect until I spoofed the $_SERVER vars before I included wp-load.php:

define('WP_USE_THEMES', false);
define( 'DOMAIN_CURRENT_SITE', $siteRow['domain'] );
define( 'PATH_CURRENT_SITE', '/' );
define( 'SITE_ID_CURRENT_SITE', 1 );
define( 'BLOG_ID_CURRENT_SITE', $siteRow['wp_blog_id'] );

$_SERVER = array(
    "HTTP_HOST" => $siteRow['domain'],
    "SERVER_NAME" => $siteRow['domain'],
    "REQUEST_URI" => "/",

The $siteRow array is my own site config, update the variables with what you need. No redirect, and all WP functions at your disposal! Enjoy.


I currently use a switch statement to set the DOMAIN_CURRENT_SITE

I set it up to work when deployed on the live server. The switch also sets DOMAIN_CURRENT_SITE for when I'm working locally on my development machine. (thus all the .dev top level domains)

After the switch statement, I define the other things that don't change but are MULTISITE related.

switch ($_SERVER['SERVER_NAME']) {
    case 'sub1.mydomain.com':
    case 'sub2.mydomain.com':
    case 'community.mydomain.com':
        define('DOMAIN_CURRENT_SITE', 'community.mydomain.com');
    case 'sub1.mydomain.dev':
    case 'sub2.mydomain.dev':
    case 'community.mydomain.dev':
        define('DOMAIN_CURRENT_SITE', 'community.mydomain.dev');
        define('DOMAIN_CURRENT_SITE', 'community.mydomain.com');

define('WP_ALLOW_MULTISITE', true);
define('MULTISITE', true);
define('SUBDOMAIN_INSTALL', true);
define('PATH_CURRENT_SITE', '/');
define('SITE_ID_CURRENT_SITE', 1);
define('BLOG_ID_CURRENT_SITE', 1);

Of course this wp-config.php file has more settings in it, but I didn't include those because they aren't specific to WP MULTISITE installs.

Note: When working locally (after downloading the latest sql file. I need to do massive search-replace operations on the database, it's slightly unrelated to your specific question, but I'm going to leave the wp-cli.org snippet below)

wp search-replace 'community.mydomain.com' 'community.mydomain.dev' --network --dry-run
wp search-replace 'sub1.mydomain.com' 'sub1.mydomain.dev' --network --dry-run
wp search-replace 'sub2.mydomain.com' 'sub2.mydomain.dev' --network --dry-run

Your Answer

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

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