Multiple WordPress Blogs On One WordPress Core
Last Saturday I had the opportunity to listen to Mike Krotscheck talk about techniques for advanced WordPress installations. One of the most interesting things I took away from his talk was a way to install multiple WordPress blogs on one WordPress core. Note we’re not talking about WordPress MU. When you only need 2 or 3 blogs, WordPress MU can be a bit overkill, but installing these blogs on the same WordPress core can save space and make updates easier. Here’s how you do it.
First of course you need to download WordPress and then upload it to your server. This is the easy part, and there are lots of instructions for how to do this, so I won’t go through them. You’ll also need a MySQL database, but again I won’t go into that since there are lots of resources already available that explain how to do this.
Let’s suppose we’re installing two blogs on domain1.com and domain2.com (lame I know, but give me a break). These should be pointing at the same server (your domain registrar can help you with this). If you navigate to your WordPress install, you’ll be asked to go through the installation process. Let’s start with domain1.com. Enter all your database information, but specify a custom table prefix (like “domain1_”) which will be unique to that domain. Once the installation is complete (make sure you save your password!) you need to delete the wp-config.php file.
Now if you navigate with your other domain, you’ll be asked to install WordPress again. This time, use a different table prefix (like “domain2_”). This will give you two different sets of WordPress tables in your database. The only problem is that you can’t access both of them. We’ll remedy this next.
Go into your wp-config.php file and find the line that looks something like this.
$table_prefix = 'domain2_';
We need this variable to be different, depending on what domain is being used to access your blog. Using the PHP $_SERVER super global, we can do this quite easily.
Here’s the new code you’d want to use.
if ( 'domain1.com' == $_SERVER['HTTP_HOST'] ) {
$table_prefix = 'domain1_';
} else {
$table_prefix = 'domain2_';
}
So now your server will display two different blogs, depending on what domain is used to access your site. Again, the advantage here is that you only use the space of one set of WordPress files, and you only have to do one update when a new version is released.
Mike talked about some other cool things you can do with this, and talked about how you can address issues with plugins like Google SiteMaps. He promised to post his slides, so if you’d like to learn more you should check out his blog.
Incredible – I just stumbled across this trick earlier today, then I check my feed reader, and here you are talking about it already:). You beat me to it.
I’ve been toying with the idea of one interface to make posts to multiple bogs, and, while I haven’t been able to come up with anything useful, you can get some really interesting results if you change the $table_prefix value later in the load series – like at the plugins_loaded action.
I haven’t taken the time to really figure out what is happening, but if you change it there, you’ll get a blog with settings that match the tables from the prefix in wp_config, but posts from the prefix that you set at plugins_loaded. It’s almost eerie how you can get the info from 2 places, but everything still works. Even the front end will pull from the second tables, and load properly.
As with so many of the other things I discover, I haven’t found anything useful to do with it yet – but it’s a fun experiment anyway.
That’s an interesting idea, but I really think it’s something that would probably best be avoided.
Changing a setting variable in the middle of the request seems like it could cause more trouble than it’d be worth.
Instinctively, I’d agree – and I’m not throwing it up on any production sites at the moment – but if you get a chance, give it a try – I haven’t been able to get anything to misbehave yet!
As for the particular project I mentioned, it looks like it’s not going to do the trick – however, I’m experimenting with the idea of switching the $table_prefix on the post_save hook, running wp_insert_post(), and switching it back when it’s done, because I need to duplicate posts in certain situations.
If anything interesting comes of it, I’ll let you know.
Just thought I’d follow up – the project I was working on ended up making heavy use of switching the db prefix (with $wpdb->set_prefix()), and it works great. The key is to hook in right before you need to use the other db, and switch it back right after – as long as you dont do either of those things too early or late, it works like a charm.
However, I can’t think of a single reason this would be useful outside of what I was using it for, which was using a single wordpress admin panel to manage a collection of remote wordpress installs. It was a fun exercise regardless.