wesg header image 3

Use 1 database for 2 blogs with WordPress 2.6

August 15th, 2008 · 12 Comments

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]

Category: Wordpress
Tags: , , ,

Similar Posts

  • http://unitedstatesofmartin.net Martin Jacobsen

    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.

  • http://pupungbp.erastica.com pupungbp

    wow, nice !!! thanks for sharing… is this working also on WordPress 2.9.* ?

    regards,

  • http://www.wesg.ca wesg

    @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.

  • http://pupungbp.erastica.com pupungbp

    Currently testing it on 2.9 will inform you the result !! again thanks !!!

  • http://pupungbp.erastica.com pupungbp

    It works !!!! with WP 2.9 :)

  • Ian

    This works with tons of WP versions, just not 2.9.1

  • http://nicholashundley.com Nicholas

    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.

  • http://www.wesg.ca wesg

    @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!

  • Edu

    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.

  • Jason

    Wes

    Will this erase the content on my current database if I make these changes?

    thanks

  • Anonymous

    Jason, it shouldn’t delete anything about content, but keep in mind that the whole point is to share the same table, so if you delete anything from either blog, it will be reflected in the other.

  • Dilana

    Is there any solution like this for wordpress 3.1+ ?