Home > Perl, WordPress > Keeping Track of WordPress Installs with Perl

Keeping Track of WordPress Installs with Perl

January 20th, 2009

I’d taken a bit of a break from WordPress coding during the development of 2.7 and instead have been concentrating on simplifying the management of multiple WordPress installs. WordPress Mu offers is great for running multiple blogs for multiple users but sometimes it just doesn’t offer your users enough control over their own WordPress site. I’m going to post a number of tips and Perl snippets, detailing how to ease the management of your scripts.

Your first step is to be able to easily list the installs you have on your system. You could maintain a list but it’s easier to let MySQL do the hard work for you. Make sure your WordPress databases identifiable by name alone.

The following Perl snippet illustrates how you might list all your WordPress installs.

$sql = "show databases like 'wordpress_\%'";
$sth = $dbh->prepare("$sql");
while($row = $sth->fetchrow_arrayref){
	($name) = @$row;
	print $row , "\n";

It’s useful to be able to map your WordPress installations to their location on disk. You could record the location of each installation, but it’s probably better to have a standard location. For instance, each install could be found under /home/<sitename> or /var/www/<sitename>.

Here’s the Perl I use to map my database names to directories. The hash allows me to override the standard mapping for a few exceptional installations.

my %dir_exceptions = (
      "foo" => '/var/www/foo/wp',
      "bar" => '/home/bar/public_html/blog',
sub wordpress_location {
      my $name = shift;
      $name =~ m/wordpress_(.*)/;
      $name = $1;
            return $dir_exceptions{"$name"};
      } else {
            return "/var/www/$name/";

James Davis Perl, WordPress

Comments are closed.