Thursday, September 13, 2012

Multi-Blogging in Wordpress

Goal:  Install Wordpress software one time, but allow multiple blogs (multi-blogging in wordpress) to be deployed within a domain.  One set of software, one set of plugins/themes, but separate posts/pages and upload data for each blog is required.

Reason: I have a domain and wanted to have more than one blog.

(Specifically written for my pal, Justin Hunt.)

1. We assume the blogs will be created using the following steps:

  • Make a sub-domain in your Apache settings.

  • Make a database using phpmyadmin or other means.

  • Follow the naming conventions below (or something similar).

2. Add this code to your wp-config.php file in above where the normal stuff is stored.
$host = $_SERVER['HTTP_HOST'];
$parts = explode('.',$host);
$domain = $parts[0];
$db = "wp_" . $domain;
$dbuser = "wp_" . $domain;
define('DB_NAME', $db);
define('DB_USER', $dbuser);
define('DB_PASSWORD', 'blahblahblah111');
/** MySQL hostname */
define('DB_HOST', 'localhost');

The “domain” variable is the subdomain prefix (for example “blog.example.com” breaks into an array called “parts” with the “explode” command.  Therefore, parts[0] is set to “blog”, parts[1] is “example” and so on.

I used a naming convention with my Wordpress databases in mysql so the database name is “wp_blog” or “wp_<subdomain>” and the user is also the same as the database name.  For simplicity the password is always the same no matter which blog I’m using.

3. Once the blog launches, make sure you have made a sub-directory under the wp-content/uploads  directory for each sub-domain.   This helps to keep the uploaded stuff separate.  For example:
wp-content/uploads/blog1
wp-content/uploads/blog2
wp-content/uploads/blog3

Note: Don’t forget to change the Wordpress Settings->Media to reflect where the uploads data gets stored for each blog!

This makes it easy to maintain the blog system software on your site.  Once you install a plug-in, the plug-in is there for any blog you have.  Same for themes.  But uploads are kept separate.

2 comments:

  1. Wow. This is cool. I am going to give it a bash, and see how it goes.

    ReplyDelete
  2. Note that as of Wordpress 3.5 some different code is needed for specifying a unique wp-content/uploads directory. In your wp-config.php, add some conditional code with the following parameter:


    // After the code from the example above:
    if ($host == "www.myhost.com")
    {
    define( 'UPLOADS', 'wp-content/uploads/'.'www_myhost_com' );// Puts uploads here for this website.
    }

    ReplyDelete