Technology Blog »

Two Wordpress blogs, one site

[ Edited 8th June 2008: I'm not doing it this way any more. Read this more recent post for the lowdown. ]

It’s a frequently asked question – how do you get multiple blogs from a single installation of Wordpress? And the answer is surprisingly simple, if a little technical.

I wanted to use a blog for this site’s portfolio page. I already had WordPress installed, obviously, so I didn’t want to install a second copy and have to maintain it in parallel. That seemed stupid.

I read the WordPress page about multiple blogs and was inspired to make my own attempt. My solution, unlike similar ones linked from that page, does not involve symbolic links: my hosting does not permit them, and anyway I realised they aren’t actually necessary.

To start with I made a new directory “portfolio” and put a new index.php file in it. This index.php is like the standard WordPress one except that it loads the rest of the code from the “journal” directory instead of the local one. The change to do this involves only one line of code.

At this point you can run this PHP file and see it load the same blog as in the normal directory, which is rather useless but shows all is well.

WordPress needs to detect the directory it’s being invoked from and select its database settings accordingly: the code for this goes into config.php. I chose to set it up so that if it’s invoked from the “portfolio” directory a different table prefix is used.

Running the PHP file now from “portfolio” gives a different result: it is suggested that you run the install script to create the database tables for your new blog. But if you attempt to do so you’ll be disappointed as it returns a 404 not found error.

This is because only the index.php is present in the portfolio directory: all the other scripts such as the administration and installation ones are elsewhere. But this problem can be solved very easily on an Apache site: with a custom .htaccess file you can send all requests for any file except index.php to the “journal” directory instead.

Once that’s working the only remaining task is to theme your new blog. If you use a completely different theme to your existing one, there’s no problem, but if you want, as I did, to use the same theme with a few minor variations then you’ll need to insert tests for the request directory into your theme code.

(If anyone wants the code for this solution I’m happy to supply it — after I’ve tidied it up a little. Just let me know.)

Share this post:
  • Digg
  • Technorati
  • del.icio.us
  • StumbleUpon
  • Facebook
  • Sphinn
  • TwitThis

15 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. July 2, 2007 4:11 am

    Hi there,

    I would like to know the code snippets you used for this, I’m not worried if it’s untidy.

    Thanks for your help,

  2. July 2, 2007 9:33 am

    OK, Ant, here goes:

    In the home directory for my new blog, I created an index.php like this, pointing to my existing blog directory:

    <?
    define('WP_USE_THEMES', true);
    require_once('../journal/wp-blog-header.php');
    

    In the same directory I created a .htaccess to route other requests to the existing blog:

    RewriteEngine On
    
    RewriteCond %{REQUEST_URI} ^/portfolio$
    RewriteRule . /portfolio/ [L,R=301]
    
    RewriteCond %{REQUEST_URI} !^/portfolio/$
    RewriteCond %{REQUEST_URI} !^/portfolio/index.php$
    RewriteRule ^(.+) /journal/$1
    

    Then I edited wp-config.php so it specified a different table prefix for my new blog, changing the assignment of $table_prefix to this:

    if(substr($_SERVER['REQUEST_URI'], 0, 10) == '/portfolio') {
      $table_prefix = 'pf_';
    } else {
      $table_prefix  = 'wp_';
    }
    

    At that point I requested the new blog and got the installation dialog. After following it my new blog was basically ready, except for its theme. I wanted to re-use most of the existing theme, so I simply edited the files to test the global $table_prefix wherever I wanted different output.

    For just two blogs this approach is OK. I’d tidy it up before I added any more.

  3. YWAMdan Says
    October 12, 2007 12:24 pm

    Hey Alfred. Groovy solution.

    I just installed Wordpress twice in order to get 2 databases in 1 site. And then I saw your solution. Which gets me to wondering… Why don’t you have a link to your Portfolio page in your top navigation menu?

    Is there a way to dynamically “inter-link” these so that this main site links correctly to the /portfolio/ page and the /portfolio/ menu links back to this main site? Or must one use static, plain html links?

    Cheers!
    dan

  4. October 12, 2007 1:26 pm

    Dan, my menu is hard-coded rather than generated by WordPress; the absence of the portfolio link is for historical reasons – and probably should be fixed.

    Your question is a good one. Making the two areas aware of each others menus would be tricky, though not impossible. A plugin which produced a composite menu would seem like a good answer.

    One flaw in my solution I didn’t mention above, but discovered since, is that you need to be careful not to let WordPress write to the .htaccess file once it is set up – it can break things! So I set mine as read-only, which means somewhat more effort when a change is required.

  5. October 19, 2007 10:20 pm

    Ooh, this looks like just what I’m looking for. I was worried what I wanted might not even be possible, but then I realised that somewhere out there someone would have written it and it looks like I was right :)

    Shall have to give it a try over the weekend. Thanks very much for sharing your code.

    Kx

  6. October 20, 2007 12:55 pm

    Kathleen, glad to be of help. If you have any problems getting it working, please let me know.

  7. October 22, 2007 9:16 pm

    Thanks for responding and for the offer of help, Alfred.

    At first I wanted to try this with an existing blog, where the WP installation is at the root level (well, it’s in the public_html folder, does that count?), so I tried to edit the .htaccess to point to the root, but I must have got that wrong as I got 404 errors.

    So now I’ve started afresh with a new installation at another domain, for testing purposes, the original at http://kathleenbright.com/wordpress/, with the ‘portfolio’ one being http://kathleenbright.com/tvt/ However, both are showing the same blog – which is the wordpress one. I looked at the database and only wp_ tables have been created. So it looks like I’ve somehow got errors in my tvt .htaccess and/or my wordpress wp-config.php files Aagh!

    Am close to giving up as I’m in way over my head and don’t want to be bugging you with numerous requests to help, especially when I’m so clueless. Would be great if it’s something small I’ve managed to miss by some n00b oversight…!

    Thanks again,
    Kx

    - wordpress/wp-config.php

    - tvt/.htaccess
    RewriteEngine On

    RewriteCond %{REQUEST_URI} ^/tvt$
    RewriteRule . /tvt/ [L,R=301]

    RewriteCond %{REQUEST_URI} !^/tvt/$
    RewriteCond %{REQUEST_URI} !^/tvt/index.php$
    RewriteRule ^(.+) /wordpress/$1

  8. October 22, 2007 10:17 pm

    Kathleen, I bet the problem’s in wp-config.php, which didn’t show up due to its content being filtered out. You’d need to run it through a converter to turn it safely into HTML entities (as I did with the code I posted). It’d be quicker if you simply emailed me the files, to alfred (at) likemind (dot) co (dot) uk.

  9. October 28, 2007 10:55 am

    Thank you so much; I’ll email it you now.

    Kx

  10. Joe Says
    January 8, 2008 5:12 am

    This is a great bit of code, great explanation and it works like a charm. Thanks!

  11. Awk Says
    April 13, 2008 6:01 pm

    I have istalled wordpress twice to 2 of its own folders (for 2 seperate blogs)in public html folder, the 1st blog worked well with permalink code being edited in the htaccess file fine, now I have added my second install for my second blog I can’t get pretty permalinks to work etc. My htacces file resides at root of my public html foder but outside both of my blog installs. Like I said all works fine on the first blog, but not on the 2nd once its installed. Is there some code I need to put in the htaccess file or similar please.

    Thanks
    Awk

  12. April 13, 2008 7:31 pm

    Awk, without seeing the content of your .htaccess files I really can’t say what’s happening.

  13. Jules Says
    November 14, 2009 11:54 am

    Awesome, Alfred! Thanks a lot for sharing this!

    Here’s a question, though: how would I include the_excerpt of my second blog into the sidebar of the first one? I mean – sure this is possible with a few lines of proper php, but I was wondering if there is a way to use the comfy WordPress shortcuts. The way I see it, I would have to tell the_excerpt where to look in this special case without screwing up search results or archives overwiews in the first blog… Tricky? Or doable? I have no clue where to start. :-)

    Thanks again!

    Jules

  14. November 15, 2009 2:16 pm

    Jules, thanks for your comment. I don’t use this approach any more, preferring to use categories to separate the different blogs. This blog, my portfolio and the articles section of this site are all really a single blog with three categories of content.

    I find this to be a much more flexible solution.

Links to this post

Some HTML is OK

or, reply to this post via trackback.