How to Easily Manage Multiple WordPress Sites With One Database and One Code Base

Content/Tools

How to Easily Manage Multiple WordPress Sites With One Database and One Code Base

Updated with security fixes

We create a lot of websites at Sitening. Although most of our clients use our yet-to-be-released custom content management system (CMS), most of the micro-sites and blogs we build use WordPress. Depending on the client and the strategy, we can sometimes build twenty or more WordPress-driven sites. That used to mean that we had to have individual folders for each site, with individual installs of WordPress. We were able to use the same the database by using table prefixes, but updating to a new version of WordPress was always a hassle — at least until now.

Scott Holdren wrote a tweak for WordPress’ wp-config.php file that will automatically support multiple websites. The trick is with the table prefix. By automatically detecting the incoming server name (the domain), the config file can either create a new database structure or reference the structure if it already exists.

Central WordPress Installation

Create a folder on your hosting provider and name it whatever you want (“wordpress” is probably a good name!). Copy the WordPress files into that folder. Then rename the wp-config-sample.php page to wp-config.php and follow the next step.

Edit WordPress Config File

Warning: Although we’re confident with the code we’ve written, we’ve only tested this on Pair Networks and we’re not responsible if you manage to blow up your WordPress sites!

In order to make this change, find and open your WordPress config file (wp-config.php). Fill in the details, like database information, and then find this line of code.

$table_prefix = ‘wp_’;

Replace that code with this code.

// You can have multiple installations in one database if you give each a unique prefix
$domain_list = array();

// auto database name
$domain_list["yourdomain.com"] = “”;

$domain_name = preg_replace(“/^www\./”, “”, $_SERVER["SERVER_NAME"]);

if (array_key_exists($domain_name, $domain_list))
{
$table_prefix = $domain_list[$domain_name];
if (!$table_prefix) { $table_prefix = “wp_” . md5($domain_name); }
}
else
{
print “Unknown error”; exit;
}

With this simple solution, you can now have one code base for WordPress on your server that all of your WordPress sites utilize. Keep all of the themes and plugins in that one WordPress folder and then use the WordPress Web admin to configure each site. Additionally, when it’s time to upgrade WordPress, you only have to update the files in that one folder and all of your sites will be upgraded.

You can also copy this code using the Snipplr entry

Related Posts Plugin for WordPress, Blogger...

Tell us what you think

  • Arpita

    Hi Ben and Prafulla,
    I also want something similar to your requirements, so can you please let me know if you find some solution?

    You can email me on patelarpita4@gmail.com

  • Prafulla

    I want to create multiple sites – but with same contents.
    I want to have master store with products. Now each affiliate will have separate websites with same product (but different prices). This different price – can be then managed through custom field. Each affiliate may also have different theme.
    How can I use multisite feature of wordpress to achieve it.

  • ben

    So, does this do everything that wpmanagerDX does? I want to be able to load up a new site with the same settings and plugins, but have a different theme and ofcourse content for each new site.

  • Cargo

    I’ve encoutered this problem when using this code.

    Parse error: syntax error, unexpected ‘^’ in /home/username/public_html/addondomain.com/wp-config.php on line 69

    What does it mean?

  • Jon Henshaw

    Hi Lee, if you’re parking a domain on another, then it will just redirect to the domain it’s parked at. On Pair Networks, if you want to use the code mentioned in this entry to have two websites with one install base, you have to add the second website as it’s own site (not parked) and ideally give it its own IP address. The key though, is to map the second website to the same folder. In my setup, I have a folder called “wordpress” and I map all of the domains to that folder.

  • Lee

    I’ve tried this, and although there are no errors, when I access the second domain it just shows the first domain – there’s no wordpress set-up or anything as I would have expected.

    I’ve parked the second domain on top of the first to point it. Is this correct?

    Also, in the database I was expecting the tables to be set-up with the prefix wp_mydomain.com, but instead I get wp_8e22a7af6221b508e768d6b27209c72e.

    I’m obviously doing something wrong!

  • http://www.randifer.com Randifer

    This is similar to the tweek that some use for joomla. Recreate an extra set of tables in the DB with a different prefix and change the config file in your secondary domains to look at the other tables rather than the originals. It really only reduces the number of databases you have, but does very little for cross site sharing oc the content :-(

    What would be interesting, and maybe something to consider for your upcoming CMS is a single database that can be accessed by multiple URL’s, in a way that is basically making the CMS operate as a quasi client/server package.

    Let each URL contain the config and presentation files, templates, site name, and let each site select it’s front page information including menu, and article criteria. but let all of this be sourced from a common DB.

    Make the distributed presentation setups responsible for maintaining the current URL, but allow the content components to be sourced centrally.

    Why?

    Because when a group has a series of somewhat related sites, content is rarely unique to a single site. It can, or could be used by many of them, IF there is a straight forward way to access the content.

    For instance, someone may have a series of sites about traveling, including one that is about car trips, another about family vacations, and another about traveling through the south west US. An article about traveling by car with your kids through Arizona would be great for all three sites, (but not the site about romantic couples getaways to New York ;-)

    A CMS/Blog that would allow the articles to be central, and have broad selection criteria for inclusion directly in distributed sites using the CMS/Blog templates and formats would be a GIANT step forward in this field.

  • http://web-o-matic.co.nz Morgan

    Love you guys for sharing this tip! Takes the headache out of regular upgrades.

    Thanks alot!

  • http://golfgearselect.com Dan

    Can this solution be used for multiple WP installations on the same site?

    For example, I have multiple blogs (multiple subdirectories), i.e. mysite.com/blog1, mysite.com/blog2, mysite.com/blog3, etc. on the same site. But, I want to combine the subdirectories to use one database (and theme, and plugins, etc.) Will this solution work?

  • Ryan

    Also, on Snipplr, you recommend to “point to” the new wordpress directory from the other domains. What method do you recommend to accomplish this?

    I tried to mimic the traditional index.php but with a path to /wordpress instead.

    ex. index.php:
    require(‘/home/wordpress/html/wp-blog-header.php’);

    While this accesses the database correctly, all images and links to wp-content wp-admin and more are broken.

    I haven’t tried htaccess.

  • http://www.jobcrowds.com Suresh

    I got this error message when I run the install script
    Warning: Unexpected character in input: ‘\’ (ASCII=92) state=1 in /mnt/w0604/d02/s31/b02f90cb/www/wp-config.php on line 60

    Warning: Unexpected character in input: ‘\’ (ASCII=92) state=1 in /mnt/w0604/d02/s31/b02f90cb/www/wp-config.php on line 61

    Warning: Cannot modify header information – headers already sent by (output started at /mnt/w0604/d02/s31/b02f90cb/www/wp-config.php:60) in /mnt/w0604/d02/s31/b02f90cb/www/wp-admin/install.php on line 36

    Screenshot;http://i40.tinypic.com/28k61yd.jpg

  • http://natetronn.com Natetronn

    Let me get this strait if I may:

    So you can clone one “Full” working WordPress site by just adding this code to the second “Empty” working wordpress site’s wp-config and this second site will look and feel like an exact copy of the first one?

    All updates made to the first will be automated to the second?

    What about the themes and plugins? Is this just to pull the database?

    Also I assume you need to change “yourdomain.com” to the first or “Full” domain?

  • Sly

    It seems to be working… it’s the most graceful solution I’ve seen yet. Thanks!

  • Tony

    I’m also on Pair, so this is attractive! However, I want to run multiple blogs (for individual country areas) within one website. These would be in sub-directories of the main domain (as opposed to sub-domains). Any idea how I could do this using your script?

    Tony

  • efoc

    This is great idea! But how can I use two or more domain for one WordPress? I use DirectAdmin.