Multiple blogs, one wordpress install…

May 6, 2008

This is not a new idea however the wrinkle for me is that I figured this out after running multiple separate installs of wordpress (and Lyceum actually) for a while. Upgrading wasn’t a real priority (which I know is an invitation) not that any of the blogs are wildly popular (one gets a bunch of attention from some horse folks but they’re hardly the hacker crowd).

The short story.

If you have existing blogs in WordPress and you’ve set them up with either separate databases or the same database with distinct prefixes for tables you can use the following to manage all blogs out of the same install of WordPress. This works as is for subdomain or distinct domain blogs, for subdirectory blogs you’d have to pull the URI server variable and match on substrings for this to work.

1) pick an existing wordpress install to do this to or install wordpress somewhere new and direct all domains and subdomains to point to whatever directory the install of wordpress is in (in my shared hosting situation CPanel was used to point everything to the wordpress install existing in the webroot on my service). Make sure all plugins for all blogs are installed in this instance of wordpress.

2) modify wp-config.php in the following way
NOTE this is only a part of the file, the part governing the table prefix for MySQL tables.

/* case statement based on code from this thought
http://me.mywebsight.ws/2006/08/11/host-multiple-wp-sites-on-one-installation/ 
(appears to be dead at the time of posting) look in the comments as well */
// table prefix, for hosting multiple intalltaions in the same db.
switch ($_SERVER[’HTTP_HOST’]) {
case “subdomain1.mydomain.com”:
    $table_prefix = “wp_blog1_’;
    break;
case “subdomain2.mydomain.com”:
    $table_prefix = “wp_blog2_’;
    break;
case “myotherdomain.com”:
case “www.myotherdomain.com”:
    $table_prefix = “wp_other_’;
    break;
default:
    $table_prefix = “wp_main_’;
}

3) Login to the admin of each existing blog (before you muck up the subdomains and directories they’re pointed to) and go to the settings page, change the URL to the new one in general settings don’t update until everything else is in place. Once you’ve got the wp-config.php sorted and set your DNS settings correctly then it should all just work. If you’re upgrading from older to newer WordPress installs don’t forget to go to the upgrade script in Admin first before doing anything else. yourdomain.com/wp-admin/upgrade.php (I believe).

An alternate (or addition) to the above code geared towards multiple prefixes in the same database could be modified to be a case statement wrapping the setup of the database variables themselves accommodating more than one database setup. As in the following.

// ** MySQL settings ** //
switch ($_SERVER['HTTP_HOST']) {
    case "subdomain1.mydomain.com":
        define('DB_NAME', 'database name');    // The name of the database
        define('DB_USER', 'database user');     // Your MySQL username
        define('DB_PASSWORD', 'database users password'); // ...and password
        break;
    case "subdomain2.mydomain.com":
        define('DB_NAME', 'database name');    // The name of the database
        define('DB_USER', 'database user');     // Your MySQL username
        define('DB_PASSWORD', 'database users password'); // ...and password
        break;
    case "myotherdomain.com":
        define('DB_NAME', 'database name');    // The name of the database
        define('DB_USER', 'database user');     // Your MySQL username
        define('DB_PASSWORD', 'database users password'); // ...and password
        break;
    default:
        define('DB_NAME', 'database name');    // The name of the database
        define('DB_USER', 'database user');     // Your MySQL username
        define('DB_PASSWORD', 'database users password'); // ...and password
}

I can attest that the above code has dramatically improved the management of the blogs we’re running.

6 Comments on “Multiple blogs, one wordpress install…”

  1. i was searching for such type of code. I want to have different subdomains which should run from the same wordpress installed in root. I am confused about how to point a subdomain to the main domain. Do i have to set up wildcard domains.

  2. @Vinay
    The way this is working for me, I have all domain definitions pointing to the same directory where wordpress is installed. When I define a domain or subdomain in cpanel I have to pick a directory visible to the web server to be the source of files for the domain or subdomain. For this trick all my domains and subdomains have the same single directory where wordpress is installed defined for them.
    This is different from pointing all domains to one domain, this trick can not work if everyone is viewing the site through the same URI.
    I prefer to setup explicit URI references to prevent stray wp blog creation (as opposed to the wildcard domain definition you’re suggesting). If you check the code at the linked article and particularly comment number 27 you’ll see why I chose to do what I’ve done.

  3. @michael
    Thanks for the reply. In my control panel when i create a subdomain it automatically creates a folder. I have no option to select a folder.

  4. Brilliantly simple! I wanted to host two blogs that share themes, code and… well… everything except the actual post data. This is the simplest way. Thanks.

  5. Pingback: thinking geek » Blog Archive » Multiple blogs, one wordpress install…

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.