Taffel.se is built from multiple blogs. The magazine itself is a blog (technically speaking), as are of course the actual blogs associated with it, like Kort om gott and Matälskaren. Less obviously, the recipe database and (coming soon) kitchen tip database are also WordPress blogs (more about how those two are integrated with Taffel.se in a later post). Running lots of blogs with separate WordPress installations would be a huge administrative headache, but fortunately it isn’t necessary.
Running multiple blogs with a single WordPress install is well documented elsewhere, and now there is even a plugin for it (which I haven’t tried).
Why not WordPress MU? First, I’m not doing what WPMU is meant for, namely hosting large numbers of independent blogs. We run an online magazine that is integrated with a relatively small number of related blogs. We do not allow users to create their own.
Second, WPMU development lags a few versions behind WordPress and is less documented.
Finally, I couldn’t get it to work when I was first considering it. WPMU required wildcard dns support which our hosting provider didn’t offer (I write required, since that was a year ago and perhaps this has changed) and which was difficult to get to work in my development enviroment. Read more about this in this interview (in Swedish, sorry) with me.
Anyway, the basic trick is to have each of your blogs point to the same directory and then identify them from the request uri in your wp-config.php.
I use the following code (database table names have been changed to protect the innocent):
$hostparts = explode('.', $_SERVER['SERVER_NAME']); switch ($hostparts[0]) { case "iblog": $table_prefix = 'wp_iblog_'; break; case "youblog": $table_prefix = 'wp_youblog_'; break; case "weallblog": $table_prefix = 'wp_weallblog_'; break; default: $table_prefix = 'wp_'; } }
The $hostparts stuff is to make the code server-neutral so it will work in my development environment, but basically it just looks at the server name from the request and sets the database table prefix based on that. Once you have this set up you can add new blogs simply by adding cases in the switch block and then pointing the desired url at your WP root (that’s how this blog works). The tables will be set up on the first visit.
If you want the blogs to share the same user database, you need to add the following as well (somewhere in wp-config.php):
define('CUSTOM_USER_TABLE', 'wp_users'); define('CUSTOM_USER_META_TABLE', 'wp_usermeta');
Unfortunately here we also have to make a small change to actual WordPress code to get things to work, which adds an unwanted step when upgrading WP. (Note to self: see how the wp-hive folks get around this) Find the following line in capabilities.php:
$this->cap_key = $wpdb->prefix . 'capabilities';
and change it to
//modified for Taffel.se $this->cap_key = 'wp_capabilities';
Note that there is no way to limit access on a per-blog basis. (UPDATE: Actually, there is, see next post…) Also, cookies are not shared, so you will have to log in to each one separately. I’ll describe a fix for this in a later post. (UPDATE: Here’s how to enable single login.)