Archive for January, 2009

04th Jan 2009

Consolidated WordPress

This blog hasn’t gotten much love since the 52 Churches project started.  I was really tempted to just delete it.  Quite frankly, keeping track of multiple installations of Word Press gets old.  Don’t get me wrong, I love WordPress.  We’ve been pretty happy with our hosting service from BlueHost, and the blog has been a good outlet for Jen, but maintaining 4 (now 6) of these monsters was getting old.

I needed a better solutions.  A little digging on-line came across an approach at This n’ That for modifying wp-config.php to adjust the prefix. It was close, but not quite what I wanted.  Basically, Mr. Kaplan was advocating adding some PHP code to the wp-config.php file that looked like:
// table prefix, for hosting multiple installations in the same db.
$table_prefix = ereg_replace('^(www\.)', '', $_SERVER['HTTP_HOST']);
$table_prefix = ereg_replace('\.', '', $table_prefix);
$table_prefix = $table_prefix . '_';

What this does is throw away the www and any periods (full stops for those of you inclined towards the Queen’s English) in the domain name and use the rest of it as the prefix. This is fine, but it’s suboptimal when it comes to long domain names. MySQL has a limit of 64 bytes for the table name, but that would have to be shared with the stripped down FQDN and the native WordPress table names. If you have a domain name like schoolsareforfish.garriber.org, you start pressing into that limit pretty quickly. Nonetheless it gave me the inspiration I needed for the solutions I was looking for. My thanks to Mr. Kaplan for his insight.

My other complaint is that it ended up making a multi-line change in a WordPress source file, albeit a configuration file. I wanted something substantially more self contained. My solution was this: create a php file in the wordpress root directory that does the prefix determination and have it return the desired prefix. For example, you could create a file called prefixgen.php. That file might look something like this:


< ?php
#figure out *which* site we're hitting
if(preg_match("/subsite1/", $_SERVER['HTTP_HOST'])) {
$prefix='ss1_';
}
elseif(preg_match("/subsite2/", $_SERVER['HTTP_HOST'])) {
$prefix='ss2_';
}
else {
$prefix='main_';
}
return $prefix;
? >

Note: I’ve put spaces between the ‘<‘ and the ‘?’ to keep from confusing the computers

This does a pretty sloppy match. If the domain the user hits contains subsite1, it will map the prefix to ‘ss1_’. This is useful as it lets users go to www.subsite1.mysite.com, or direct to subsite1.mysite.com (assuming your DNS mappings and hosting provider are set up to support both). It does the same thing for subsite2. If nothing else it falls through to the main site (prefix ‘main_’). In this way, any DNS entry that resolves to your installation and gets to the right directory, but doesn’t map to a subsite, will go to your main site.

The update to wp-config.php becomes trivial. Change the prefix line to read:

$table_prefix = include 'prefixgen.php';

Presto! Clean and self contained. I’m a very happy camper. I now have a single directory with a single set of WordPress files serving all of our different sites. The sites are all stored in one database which makes for easier server side backups.  My “installations” at my hosting provider have become symbolic links to a single WordPress directory and, best of all, instead of having to manually update multiple WordPress installations, I just update one to upgrade.  Maybe I’ll let Jen keep this blog.  Maybe I’ll just take it over instead.  I could change the tag line to “Bits, Bytes, and Querries”

Posted in Geek Stuff, Zeteo | No Comments »