Monthly Archives: July 2010

A better way to host multiple domains on WordPress MU/Multi-Site

What am I talking about!?

Well, there is this thing called “Multi-Site” in WordPress 3.0. You can read official docs on it here: Create a Network. It used to be called WordPress Mu or WPMU. The Multi-Site platform is MEANT to allow you to host multiple blogs on one domain via either subdirectories or subdomains like:

http://www.somesite.com/joesblog

or

http://joesblog.somesite.com

But any savvy web dev knows that this is not far from being able to host say the following on the same WordPress Multi-Site platform/install:

http://joesblog.com

and

http://samsblog.com

That’s what this guide is for: to help you “hack” WordPress to get multi-domains to work. Now you can do this with some fancy redirection plugins. But I really don’t like these because they mask/redirect a subdomain which can complicate some plugins and who knows what else. That’s why I wrote this post so you can make this work without using a redirection plugin.

This is really only for smarty-pants web developers

It’s possible that one day WordPress will move/remove this code I’m modifying. They did it in 2.9 -> 3.0 and I had to go looking for that bit of code again. Sure I’ll probably update this post if they do, but for how long? As long as I’m managing a Multi-Site I suppose. You do the math. So that said, you’re probably going to want to be smart enough to debug this if I quit the internets, or you don’t bookmark my site, or monsters eat your homework, etc..

Additionally: I haven’t ran this by ANY wordpress experts. But I’ve been running this on “old” WPMU for years, and now “new” 3.0 Multi-Site, with no issues.

This is NOT for the faint of heart!

If you have a lot of sites you’re going to want to gauge heavily on how/when to do this. However in my opinion this is a LOT less janky than all the redirect plugins. Twice I reviewed my options and both times I came up using this method. Once it’s done: it’s great.

This is also the first draft.

I literally copy/pasted this from an email I just sent and made some modifications, so it may by a little rough. I’ll try to update it if people give me feedback or I do any modifications on my end.

My method is two-part

  1. changing 1 line of php (into 2)
  2. changing the “domain name” in the db for each blog to be the real domain instead of the subdomain

Part 1

note some further info/instruction can be found here on updating to new WP versions etc.: Installing/Updating WordPress with Subversion

We check out via svn on the 3.0.1 tag (note: this is the latest version as of this posting):

$ svn co http://core.svn.wordpress.org/tags/3.0.1 .

note: don’t worry about not being able to updated to new versions like 3.1 etc all you have to do to do so is: svn switch http://core.svn.wordpress.org/tags/3.1 you can see how to do that in that wp codex article I linked

once we’re checked out, we are able to make changes to the core and be able to still update from wordpress because updating from the remote repository will trigger a merge with our locally modified code. So we don’t have to fear our slight modification ever getting overwritten.

As for the code we changed, it’s very simple. WPMU is (obviously) only setup to work on subdomains and subdirectories. So it _always_ loads the basic info from the root domain name. So we only need to make a slight modification for when it does this:

Here is the .diff patch if you don’t want to do it manually: ds_wpmu.zip

file: wp-includes/ms-load.php
line: ~141

remove this line:

$current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s", $current_site->domain, $current_site->path ) );

replace with these lines:

$current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s", $domain, $path ) );
$current_site->domain = $domain;

Part 2

Then you have to update each blog so that it’s domain name isn’t the subdomain and instead is the real domain. When I first set this up I had < 20 domain so I did it all manually, you may want to do it automagically if you already have a lot of sites via a big sql script or a mixed php/sql script or w/e your favorites are. Anyway these are the options that need to be updated in each blog:

  1. in [wp_blogs] each [domain] field (and possibly [path] if you’re using subdirectories)
  2. in each [wp_X_options] table, X being the blog’s ID the following fields:
  • siteurl
  • home
  • fileupload_url

Note: Instead you can update the domain manually in wp-admin when editing the site. You can check-box the “Update siteurl and home as well.” but it doesn’t update fileupload_url, so make sure to change tht too.

And that’s it.

Naturally these instructions come with no warranty, so don’t break your stuff, and make sure to back everything up before you do anything. I’m sure some of you are looking at me in horror, but meh, get over it.