Like many people in this world, I like to put the minimum amount of effort into things. Don’t get me wrong, this is never at the expense of quality – I prefer to get the same top notch results but with the minimum effort.
On this page I will let you know how I run many WordPress blogs from just one install. This means that upgrades, plugin installations, back ups etc are a one time event across all blogs. This saves me a lot of maintenance time which allows me to get on with building my sites instead.
The Single Shared WordPress Installation Explained
There are a number of solutions to this out there, if you google around you might even find some plugins that are supposed to help manage this. I went for a very simple approach that requires a couple of configuration changes but it’s nothing that is beyond the reach of experienced WordPress users.
I have a very straight forward approach. I have one install of WordPress in the directory provided by my web host.
public_html/wordpress
- When ever you set up a new domain, or sub domain, simply point it at this directory. Hopefully your host provides you with cpanel which will allow you to set this up yourself, if all else fails ask them to point the domain to the right place.
- Then simply set the WordPress configuration variables dynamically based on the current base URL of the site being viewed. All we are doing is directing the WordPress code to look at a different database depending on what the current domain is. So each of your domains will have its own set of 10 or so tables but this is no problem and unless you make a habit of updating tables directly you’ll never even notice.
- Each time a new domain is set up I enter a new record into a second configuration file that I have created, this maps each domain to a new table prefix. When I navigate to the domain WordPress will automatically notice that the tables don’t exist yet and the installation routine will be triggered. A couple of clicks later and I’m up and running.
Alternatively you can create a set of template tables which have your preferred settings already entered, for example permalink rules, plugin installation, admin user accounts etc. This template then just needs to be run in with the correct table prefix each time you set up a new site.
Some Code Snippets
Note: I am not a PHP developer by trade, so there may well be a better way to do this!
So this what I have added to my wp-config file:
$domain = str_replace('www.','',$_SERVER['HTTP_HOST']);
include ("domainmapping.php");
$xml = simplexml_load_string($domainxml);
$key = 0;
foreach ($xml->domain as $xml_domain)
{
$xml_database = strtolower($xml->database[$key]);
$xml_prefix = strtolower($xml->prefix[$key]);
if (strtolower($domain)==strtolower($xml_domain)){
define('DB_NAME', $xml_database);
$pre = $xml_prefix;
break;
}
$key++;
}
This reads in my mapping file that maps each domain to a database and a set of tables. You might not want to use multiple databases, you can store each set of tables in one db if you like. I separate different sets of sites into different dbs to make back ups easier.
The above piece of code will replace the DB_NAME define but the prefix is set further down in the config file. So I just replaced this with the following which uses the $pre variable we set above from our mapping file.
$table_prefix = $pre.'_';
The domainmapping.php file looks a bit like this:
So this maps WP running on the blogercise.com domain to the “bloggingsites” database and the set of tables prefixed “blgrcse”
Simples.
Theme Considerations
The problem with this technique was that with a shared theme directory all of the blogs inherited any custom changes that I wanted to make to a them, the solution to this was to make multiple copies of the theme and then select the appropriate one for each blog.
More recently I have moved to selecting themes that allow design options to be configured and stored in the database. This means that each of my sites can share the one theme but things such as the headline title or page menus can be set up individually.
For example, check out:
Get in Touch
If you have any problems getting this going then please get in touch. Test this out carefully on your development environment before going live, I have had a few occasions where miss-configuration has brought all of my sites down at the same time!