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


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[""] = “”;

$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); }
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

Comments are closed on this post