Check the WordPress version of several sites at once

wordpress-logo-32I’ve got this blog of mine where I log in every now and then, so whenever there is a new version of WordPress, I see the update reminder message. There is, however, over a dozen other blogs under my wings and I don’t log into those nearly as often, in fact, I hardly log into them at all. Some more savvy admins of those blogs take care of the updates themselves, some don’t. Leaving those blogs not updated poses a significant risk, so I thought I would create a tool that would keep me informed.

It’s also possible to acivate the Automatic Background Updates, but with a few custom-made plugins in place, I prefer some degree of human supervision over the update process, either testing the update on a development machine first, or at least to be able to see if something goes wrong immediately.

So I wrote this perl script that will keep an eye on those websites for me. The only problem is that it depends on meta tag “generator” to get the WordPress site’s version, and some themes prefer to keep this information hidden.

Uning Cron, this script will email the results to your address at some convenient time, once a week sounds just about right for me.

You only need to add more URLs into the array on line 41:

#!/usr/bin/perl
#
#       .--' |
#      /___^ |     .--.
#          ) |    /    \
#         /  |  /`      '.
#        |   '-'    /     \
#        \         |      |\
#         \    /   \      /\|
#          \  /'----`\   /
#          |||       \\ |
#          ((|        ((|
#          |||        |||
#  perl   //_(       //_(   script :)

use strict;
use Data::Dumper;
use LWP::UserAgent;
use Email::MIME;

my $wp_url = 'https://wordpress.org/latest.tar.gz';
my $message = '';

# get information from $wp_url using HEAD method
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->env_proxy;
my $response = $ua->head($wp_url);


if ($response->is_success) {
	if ($response->header('content-disposition') =~ m/wordpress-([0-9\.]+)\.tar\.gz/) {
		my $wp_version = $1;
		my $wp_site_content;
		my $wp_update_url;
		my $wp_site_version;
		
		foreach my $url (
			'http://www.example.com/blog/',
			'http://www.example2.com/',
			# 'add more here',
		) {
			#print "\n$url\n";
			my $wp_site = LWP::UserAgent->new;
			$wp_site->timeout(10);
			$wp_site->env_proxy;
			$wp_site_content = $wp_site->get($url);
			if ($wp_site_content->is_success) {
				if ($wp_site_content->decoded_content =~ m/<meta\s+name="generator"\s+content="WordPress\s+([0-9\.]+)"/) { $wp_site_version = $1; if ($wp_site_version ne $wp_version) { $wp_update_url = $url; $wp_update_url =~ s/\/$//; #remove the trailing / $message .= "- $url is version $wp_site_version (update here: $wp_update_url/wp-admin/update-core.php ) \n"; } else { $message .= "- $url is up-to-date\n"; } } else { $message .= "- unable to determine WP version of $url - hidden generator tag?\n"; } } else { $message .= "- unable to determine read $url\n"; } undef $wp_site; undef $wp_site_content; undef $wp_site_version; sleep 1; } if ($message ne '') { $message = "The following sites have been checked against the current stable version of WordPress: \n\n\n" . $message; my $email_message = Email::MIME->create(
			  header_str => [
			    From    => 'from@example.com',
			    To      => 'to@example.com',
			    Subject => 'Wordpress sites report',
			  ],
			  attributes => {
			    encoding => 'quoted-printable',
			    charset  => 'ISO-8859-1',
			  },
			  body_str => $message,
			); 
			use Email::Sender::Simple qw(sendmail);
			sendmail($email_message);
		}
	}
	else {
		die 'Unable to determine current stable WordPress version';
	}
	
}
else {
    die $response->status_line;
}

And this is what the incoming email looks like (supposing there are a couple of URLs):

Subject: WordPress sites report

– http://www.example.com/blog/ is up-to-date
– http://www.example2.com/ is version 3.9.2 (update here: http://www.example2.com/wp-admin/update-core.php )
– http://www.example3.com/ is version 4.0 (update here: http://www.example3.com/wp-admin/update-core.php )
– http://www.example4.com/ is up-to-date
– unable to determine WP version of http://www.example5.com/ – hidden generator tag?