Use 1 database for 2 blogs with WordPress 2.6

UPDATE 2: A lot has changed to WordPress since 2.x so I don’t recommend trying this for versions above 4.0. I can’t confirm it’s operation.

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]

Tagged with: , , ,
14 comments on “Use 1 database for 2 blogs with WordPress 2.6
  1. 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 says:

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

    regards,

  3. wesg says:

    @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 says:

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

  5. pupungbp says:

    It works !!!! with WP 2.9 🙂

  6. Ian says:

    This works with tons of WP versions, just not 2.9.1

  7. Nicholas says:

    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 says:

    @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 says:

    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.

  10. Jason says:

    Wes

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

    thanks

  11. Anonymous says:

    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.

  12. Dilana says:

    Is there any solution like this for wordpress 3.1+ ?

  13. Eric says:

    hi there, any solution for wp 3.3.x?

  14. Yury Zherabilau says:

    Hey, Дилян!
    Here is solution for WP 3.3.2 – http://nokeywordinurl.com/2-wordpress-blogs-with-1-users-table-and-1-login/

Leave a Reply

Your email address will not be published. Required fields are marked *

*