Published on October 29, 2012 by
Daniel Pataki
I was installing WordPress as a subdomain multisite install, knowing that I would need to exclude some domains. I still wanted the wildcard subdomain option though so we could just create sites on the fly. Getting this up and running is not difficult, the problem was that I wanted to use WordPress functions on the “extra” subdomain, but not WordPress itself.
Usually this is not that difficult. Say you have a WordPress website at http://mysite.com. You can create a folder in that directory called ‘stuff’ with an index.php file. If you visit http://mysite.com/stuff the index.php file handle the display, not WordPress. What you can usually do in this situation is include the ‘wp-load.php’ file from your WordPress install and voilla, you can use all WordPress functions.
With a multisite install this isn’t the case. If you include ‘wp-load.php’ WordPress will know you’re loading the code from a subdomain which is not a part of your sites. Depending on your registration settings it will forward you somewhere. You can change the location of the forwarding, but you can’t disable it.
After mucking about in the source code I figured out that if you define the <code>$current_site</code> and <code>$current_blog</code> objects before you include ‘wp-load.php’ all will be well. WordPress will think you are on the specified site and it will not redirect you. You can then use any WordPress code or switch between blogs using <code>switch_to_blog()</code>
$current_site = (object) array(
'id' => 1,
'domain' => 'mydomain.com',
'path' => '/',
'blog_id' => 1,
'cookie_domain' => 'mydomain.com',
'site_name' => 'My WP Network Site',
);
$current_blog = (object) array(
'blog_id' => 1,
'site_id' => 1,
'domain' => 'mydomain.com',
'path' => '/',
'registered' => '2012-10-27 01:40:55',
'last_updated' => '2012-10-29 07:00:34',
'public' => 1,
'archived' => 0,
'mature' => 0,
'spam' => 0,
'deleted' => 0,
'lang_id' => 0,
);
include( SITEPATH . '/wp-load.php' );
I would like to stress that this sucks. However, it seems to be the only way to do it without actually creating a site for the subdomain. If anyone knows a better solution please-please let me know in the comments!