What is the best way to load the WP environment in a subdomain of my multisite WordPress install?

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?

4 s
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', "https://wordpress.stackexchange.com/" );
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', "https://wordpress.stackexchange.com/" );
    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.

Leave a Comment