Auto update multiple installs of WordPress with cron/ssh via bash script

With WordPress being updated more frequently than ever with new security fixes and features it’s important to keep you site up to date with the lates version of WordPress.

Running one update is easy and quick enough but if you have a dozen or more WordPress sites on one server you can update them all at once with a very simple bash script.

This script can be used in one of two ways:

1:  Setup the script and run it every time a new version of Wordpres is released

2: Setup a cron task to run the update weekly, this will mean you never have to worry about out of date versions of WP ever again.

It works by using wget to download the latest version and unzip it’s files to each of your WordPress installs.

#!/bin/bash

wget -q http://wordpress.org/latest.tar.gz -O /var/www/latest.tar.gz; 

tar -C /var/www/ -zxf /var/www/latest.tar.gz;
cp -r /var/www/wordpress/* /var/www/example.com/htdocs;
cp -r /var/www/wordpress/* /var/www/example.co.uk/htdocs;
cp -r /var/www/wordpress/* /var/www/test.com/htdocs;

Note:  If all the sites on your server are running WordPress you could replace the individual cp lines with a loop to go through folders but to keep it simple and stop errors I’ve put a line in for each WP site.

PHP Spam Blocker

If you have a problem with Bot user’s and spam in WordPress the best solution is setting up Akismet. It’s an easy to use plug-in that just needs an API key.

For Bespoke websites there isn’t a one click plug-in but there is a super easy to use Bot database called BotScout with an API that only requires a key and CURL.

Botscout is compatible a range of off the shelf PHP systems such as PHPBB.

The following function will return

  • 0 = Not a bot
  • 1 = Is a bot
  • 2 = Failed to check for some reason
function testSpam($XIP,$XUSER,$XMAIL){  /// IP,username,email
$botdata='';
$APIKEY = 'PxX19wq5uJWiIL8'; // your optional API key
$XMAIL =urlencode($XMAIL); // make the url compliant with urlencode()
$apiquery = "http://botscout.com/test/?multi&mail=$XMAIL&ip=$XIP&key=$APIKEY"; // testing for an email address and IP

if(function_exists('file_get_contents')){
$returned_data = file_get_contents($apiquery);    // Use file_get_contents
}else{
$ch = curl_init($apiquery);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$returned_data = curl_exec($ch);
curl_close($ch);
}

if($returned_data==''){// sanity check
return 2;
}

$botdata = explode('|', $returned_data); // take the returned value and parse it (standard API, not XML)

if(substr($returned_data, 0,1) == '!'){// if the first character is an exclamation mark, an error has occurred
return 2;
}

if($botdata[3] > 0 || $botdata[5] > 0){  /// it must be spam bot
return 1;
}else{
return 0;
}

}

PHP and dynamic URLs

Whenever I make a new website now I always think about what the urls will need to be like. Most off the shelf CMS platforms will have a plug in or option to enables similar functionality.  But if your writing the site yourself you will want to be able to convert URL from post Id’s to post names to Urls and back again.

I have used many methods to do this in the past but recently I found two really useful built in PHP functions. urlencode() and urldecode().

Alternatives to using urlencode() would be writing your own function to convert all the url unfriendly symbols, slashes, spaces etc. into HTML entities or just replacing them with hyphens.

Again PHP has an built in function for everyhting.

Posted in PHP.