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?
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:
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).
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.
You could give a try to WordPress Mu Domain Mapping plugin.
Update
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.Or you can do a wp_redirect after the include of
wp-load.php
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:
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.
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)