UPDATE: This also works with WP 2.7.1. If editing wp-db.php is too daunting, you can download my version, which will work as long as the prefix of your main blog is wp_.
While developing new themes for the blog, I needed a place to preview a theme that includes all the current posts, comments and categories. Thanks to a blog post from United States of Martin, I was able to set up the blog in a subdomain that only uses a different options database.
Basically the method is to tell the main blog to allow users in the slave blog to access the posts. You do that by accessing your web host’s MySQL database admin panel and entering this text as two separate queries:
INSERT INTO main_wp_usermeta (user_id, meta_key, meta_value) VALUES (1, 'slave_wp_user_level', 10)
INSERT INTO main_wp_usermeta (user_id, meta_key, meta_value) VALUES (1, 'slave_wp_capabilities', 'a:1:{s:13:"administrator";b:1;}')where slave_wp is the prefix of your second blog, and main_wp is the prefix of your original blog.
The second task is to change the file /wp-includes/wp-db.php by adding a few lines of new code. The reason I’m posting this is because even with the original instructions, I could not get it working with WordPress 2.6 for some time, even after having it work properly under 2.5.
Find this text in wp-db.php
foreach ( $this->tables as $table )
$this->$table = $this->prefix . $table;which is likely on line 383 – 384. Immediately after this text, insert these lines:
$this->posts = 'wp_' . 'posts';
$this->users = 'wp_' . 'users';
$this->categories = 'wp_' . 'categories';
$this->post2cat = 'wp_' . 'post2cat';
$this->links = 'wp_' . 'links';
$this->postmeta = 'wp_' . 'postmeta';
$this->usermeta = 'wp_' . 'usermeta';
$this->terms = 'wp_' . 'terms';
$this->term_taxonomy = 'wp_' . 'term_taxonomy';
$this->term_relationships = 'wp_' . 'term_relationships';
$this->comments = 'wp_' . 'comments';where wp_ is the prefix of your main blog as set up in wp-config.php.
This immediately fixed my setup, and now I can enjoy 2 different themes, with one set of posts, comments, categories and pages.
[tags]Wordpres, 2.6, database, MySQL, blog[/tags]

9 responses so far ↓
1 Martin Jacobsen // Aug 16, 2008 at 5:46 am
Nicely done Wes. I haven’t had the time (or need) to upgrade yet since I have a lot of customization I need to take into consideration. I’ll add your update so people don’t get confused.
2 pupungbp // Jan 6, 2010 at 5:17 am
wow, nice !!! thanks for sharing… is this working also on WordPress 2.9.* ?
regards,
3 wesg // Jan 7, 2010 at 10:13 am
@pupongbp: This should work for 2.9 as well, though I haven’t fully tested it. I will try to test it, and update the post.
4 pupungbp // Jan 19, 2010 at 11:18 pm
Currently testing it on 2.9 will inform you the result !! again thanks !!!
5 pupungbp // Jan 20, 2010 at 12:33 am
It works !!!! with WP 2.9
6 Ian // Jan 30, 2010 at 7:27 am
This works with tons of WP versions, just not 2.9.1
7 Nicholas // Mar 4, 2010 at 1:55 am
Could this ever be made into a plugin? I just don’t want to hack into the code and worry about the tweak every time I update the WP.
8 wesg // Mar 4, 2010 at 12:09 pm
@Nicholoas: Interesting thought! I suppose it could be built into a plugin, however you’d likely still have to run it after each WordPress update, though a check can probably be built in. I’ll look into it!
9 Edu // Apr 16, 2010 at 1:36 pm
Where should i exactly add this ?
INSERT INTO main_wp_usermeta (user_id, meta_key, meta_value) VALUES (1, ‘slave_wp_user_level’, 10)
INSERT INTO main_wp_usermeta (user_id, meta_key, meta_value) VALUES (1, ‘slave_wp_capabilities’, ‘a:1:{s:13:”administrator”;b:1;}’)
Thank you.
Leave a Comment