-
Remotely Managing WordPress with PHP
Posted on April 8th, 2010 1 commentWordPress is the ubiquitious platform these days although I hope to change this with the inevitable release of Silk CMS in the next month or so. In my line of work, I tend to have to install many, many copies of wordpress all over the place. WordPress makes a great platform for deploying niche sets especially and comes with hundreds of plugins that make creating these sites dirt simple. Anyone who has managed niche sites can attest that it becomes quite unwieldy to manage hundreds of websites. It helps to standardize on a common infrastructure for all of them (ie, themes, plugins, etc) however each site needs to be slightly customized to a particular niche. Plugin settings need to be tweaked, headers changed, sublines updated, etc. Uploading wordpress to each host and manually unzipping it and re-uploading everything that is non-stock is one way to go about it but it’s tedious and time consuming. If you happen to host on a hosting service that offers fantastico your job is a little easier but not by much.
In thinking of a solution, I investigated how fantastico goes about automatically installing and configuring wordpress. If they can do it, it can’t be that hard. I continued to do a lot of research into ways to remotely install wordpress and got some great ideas from some other blogs but ultimately came up with my own solution. My primary goal was to establish an easy way to replicate wordpress sites (essentially cloning them), while modifying them to a particular niche in the process. I ended up building an application that can be tasked with remotely managing many sites with WordPress being one of the dialects it is fluent in. The details of how the platform works is for another day but in a nutshell different platforms are supported using an adapter design pattern, with WordPress being one of the adapters offered. The adapter provides functionality for fresh installations, backup, restore, cloning, and remote configuration. All of this is provided and all that is required is ftp credentials.
A great place to start is by grabbing this fantastic php script called pclzip. This provides support for zipping and unzipping files without the need for a compiled php module. It provides a tremendously powerful class for interacting with zip files which gives us the ability to zip up wordpress sites and unzip them. Next we need a utility script that can act on our behalf. I have created a script called nichetool.php which implements the functionality to work on the wordpress installation directly. Whenever I need to interact with the wordpress site, my platform uploads this script via ftp (which is easy since i store site profiles which includes ftp credentials), and sends commands to this script which act on the platform’s behalf.
A backup command might look something like this: http://somesite.com/nichetool.php?action=backup
And the responding code would look something like this:
-
-
function actionBackup($pParams){
-
-
$include_content = $pParams[‘content’];
-
else
-
$include_content = false;
-
-
$dbname = $matches[1];
-
$dbuser = $matches[1];
-
$dbpassword = $matches[1];
-
$dbhost = $matches[1];
-
Here we read the contents of wp-config.php to retrieve the database credentials.
-
-
$files = _getFileList($folder);
-
-
if ($include_content){
-
$backupFile = "nichepanel-all-backup.sql";
-
$command = "mysqldump -h {$dbhost} -u {$dbuser} -p{$dbpassword} {$dbname} > {$backupFile}";
-
if ($retval > 0)
-
outputError($output);
-
-
$files[] = "{$folder}/{$backupFile}";
-
} else {
-
$backupFile = "nichepanel-config-backup.sql";
-
$command = "mysqldump –ignore-table={$dbname}.wp_posts –ignore-table={$dbname}.wp_postmeta –ignore-table={$dbname}.wp_comments –ignore-table={$dbname}.wp_terms –ignore-table={$dbname}.wp_term_relationships –ignore-table={$dbname}.wp_term_taxonomy -h {$dbhost} -u {$dbuser} -p{$dbpassword} {$dbname} > {$backupFile}";
-
-
-
if ($retval > 0)
-
outputError($output);
-
-
$files[] = "{$folder}/{$backupFile}";
-
-
$backupFile = "nichepanel-struct-backup.sql";
-
$command = "mysqldump -d -h {$dbhost} -u {$dbuser} -p{$dbpassword} {$dbname} wp_posts wp_postmeta wp_comments wp_terms wp_term_relationships wp_term_taxonomy > {$backupFile}";
-
-
-
if ($retval > 0)
-
outputError($output);
-
-
$files[] = "{$folder}/{$backupFile}";
-
}
-
I offer the option of either including the blog content in the backup or not. Content is stuff that is specific to that particular blog, records that describe posts, categories, taxonomies, links, etc. The first mysqldump call grabs the non-content tables and the second grabs the content tables without the data. This is really useful for cloning wordpress sites where you want the files and settings but not the posts and categories. Note also that a list of files that are created is maintained. This is so we can tell PCLZIP what files to archive.
-
-
-
$z = new PclZip(‘nichepanel-backup.zip’);
-
-
-
$result = $z->create($file_list, PCLZIP_OPT_REMOVE_PATH, $folder);
-
-
if ($result == 0)
-
-
echo ‘OK’;
-
}
-
Finally we create the zip file and report back that everything went fine. The platform waits for the ‘OK’ and when that happens, it can then download the backup file and do what we want with it. Restoring is simply the reverse of the backup. The platform uploads the zip file and unzips it. If the wordpress site is being cloned to another host, the wp-config.php file will need to be modified to reflect the new database credentials.
Another useful function of the utility script is to allow for post installation/copy configuration. WordPress is easy in that it maintains most of it’s configuration in the wp_options table using key – value pairs. So in order to configure wordpress remotely, we can send a command like this:
http://somesite.com/nichetool.php?action=postinstall?options[blogname]=NiftyBlog&options[blogdescription]=Blah
and respond to it with:
-
-
function actionPostInstall($pParams){
-
-
$dbname = $matches[1];
-
$dbuser = $matches[1];
-
$dbpassword = $matches[1];
-
$dbhost = $matches[1];
-
-
$options = $pParams[‘options’];
-
$db = db_connect($dbhost, $dbname, $dbuser, $dbpassword);
-
-
foreach ($options as $key => $val){
-
db_query("UPDATE wp_options SET option_value=’$val’ WHERE option_name = ‘$key’;", $db);
-
}
-
-
db_disconnect($db);
-
echo ‘OK’;
-
Again we sniff wp-config.php for database credentials and then we use those to directly modify wp_options with the options that we passed in an array. This can be used to configure wordpress settings as well as plugin settings.
This is just a sample of the kinds of thing you can add to a remote management script. A utility script coupled with a platform that persists site profiles could be potentially used to manage a farm of wordpress installations all directed uniformly to do your bidding. The possibilities are endless.
One response to “Remotely Managing WordPress with PHP”
-
Great Work!,
I would be interested in purchasing nichetool.php and having a conversation with you! Let me know if you’d be into it.
-Aaron DuChateau
Blackwire Marketing
Leave a reply
-


