WordPress Hacking: Multiple Blogs On One Set of Tables

Note: This post was written for WordPress 2.1. Please see the comments for discussion on how to apply to newer versions!
I recently rolled out a new theme for my website. Normally when I do this, I do it against the live site, so there’s about an hour where the site is either broken, or looks really weird while I’m working on it. This time I wanted to be a little smarter.
The goal was to build another blog — a “test” blog where I can try out new themes and other experiments — that pointed to the same data as the main site. This way I could get a realistic picture of what the site would actually look like before I publish an updated theme.
The more I thought about that, the more useful the concept seemed. What if I wanted an iPhone-friendly version of the site? I wouldn’t want to write the same post in two places. Now eXpression could do this very easily — since every “page” was actually a query, you could just specify a different theme file (XSL) in the query. WordPress, not so much.
What WordPress can do is allow you to specify a table prefix for each installation on a given server. That means you can have 3 blogs, each with their own set of tables. When you build your wp-config file, you tell it what prefix to use. Since this is the case, it should be fairly easy to install another instance of WordPress and point it back to the original blog tables, right? Here’s how you do it…
Step 1 – Install a new instance of WordPress

  • Make sure it has a unique folder name so it doesn’t over-write your original blog
  • Run the install pages, and give the new blog a unique table prefix
    • IE: If your original blog got the default prefix: wp_ then give the new blog newwp_
  • Get the blog up and running with an admin account, but don’t bother posting anything

Step 2 – Point the new blog to the original blog’s tables

  • Modify wp-settings.php as per the instructions found here
    • Modify all the table pointers EXCEPT for Options
  • This means that only the Options (including Theme and Plugins) will be specific to your new site. Everything else (including Posts and Comments) will come from the original site
  • So anywhere you see: $wpdb->users = $wpdb->prefix .
    Change it to: $wpdb->users = ‘wp_’ . ‘users’;

    • where ‘wp_’ is the table prefix for your original blog

Step 3 – Grant the admin user from your original blog the same permissions on your new blog, by inserting two records into your wp_usermeta table:

  • Where wp_ is the table prefix of your original blog and newwp_ is the table prefix of your new blog…
    • INSERT INTO wp_usermeta (user_id, meta_key, meta_value) VALUES (1, 'newwp_user_level, 10')
    • INSERT INTO wp_usermeta (user_id, meta_key, meta_value) VALUES (1, 'newwp_capabilities', 'a:1:{s:13:"administrator";b:1;}')

That’s it! You now have two uniquely styled blogs with the same data in them! The only thing that sucks is if you have Sidebar widgets configured, you’ll need to re-configure them in your new blog as well and manually keep them in sync. That’s the case for all options, and that’s why this is useful.
A couple other notes:
If you want, you can delete the superfluous “new blog” tables — just make sure you leave the Options table intact.
I have no idea how this will work during a WordPress upgrade. My current plan is to run the upgrade on the original, or base, blog, then manually change the Options table structure, if necessary, for the secondary, or derived, blogs.

A stranger with the door key, explaining that I'm just visiting…

I complain a lot about the US$. I thought today I’d explain why.
When we first considered moving to the States — easily 4 years ago, the prospects were great. Not only do US employers pay significantly better, but they pay in US dollars, which meant we could make even more on our money!!
This was the assumption upon which we lept southward. Although the first part was true — my pay in the States is a good 50% better than what I was making in Canada — the second part is sounding more and more foolish.
Although we are most liquid here, we still have plenty of liabilities in Canada: two car loans, life insurance, and student debt. These payments must be made monthly. Did you know there’s no easy way to transfer funds between countries? In the movies the bad guys wire each other money with the click of a mouse and an animated ticker counts off their millions as its deposited. In real life, its nothing like that.
The reality is that once a month, Nicole heads down to the bank — during their ridiculously limited business hours — and writes up a wire transfer. This takes anywhere between 3 and a billion days to go through. Meanwhile the US bank charges for the money order and shaves their share for doing the currency exchange. Then the Canadian bank charges for the transaction and shaves their share for doing the currency exchange.
When we first moved here and the exchange rate was hovering near 1.15 CDN$ per 1 US$ we still managed to make (Canadian) money on the deal. We’d meet our Canadian obligations, and tuck a little money into our account for savings and for trips home.
Then the exchange rate dropped to 1.07ish. We stopped making money on the transfer, but at least we were still breaking even. Yes, 1.07 is the same as “at par” when banks are involved.
Then the exchange rate dropped to 0.98 US$ per 1 CDN$.
Dear US Currency: welcome to the short bus. For years you looked at the Canadian dollar as if it was your retarded little brother. Now the rest of the world looks at you the same way.
Even after a slight recovery, putting the US$ back on top by fractions of a cent, we’re still hemorrhaging cash. We’re going into the hole each month we transfer money back to Canada.
I’ve gotta be honest, if we were to take an objective look at our foray into the States right now, the results that come back would not be optimistic… The economy continues to crumble, the war shows no signs of ending, and most of the reasons we had for moving here are gone.
On the plus side, shopping is still better here. Did you hear they want $799 for an iPhone in Canada? Am I the only one that’s noticed that the Canadian dollar should buy as much kit as the US dollar right now?