Sep.10

WordPress Version Checker

With the big security release last week, I had a hard time remembering all of my WordPress sites. Some, like my blog, are obvious. But I also use WordPress to build affiliate sites (like WNY Wine Tastings), and even I don’t recognize they’re WordPress sites. So I figured I could write a script to not only find all of the WordPress installations on my GoDaddy server, but also tell me which are outdated.

This script isn’t commented as nicely as some of my other ones, but it’s only 40 lines long, so I’m sure you can figure it out.

	set_time_limit(0);

	// What's the current WordPress version?
	$cHTML = file_get_contents('http://wordpress.org/download/');
	list( , $cHTML) = explode('latest stable release of WordPress (Version ', $cHTML);
	list($fVersion, ) = explode(')', $cHTML);
	$fVersion = trim($fVersion);

	// echo("Current version:\t$fVersion\n");
	$cMsg = '';


	// Get a list of the sites
	$aResults = explode("\n", shell_exec('find /var/www/vhosts -name "wp-config.php"'));
	array_pop($aResults);	// Blank

	foreach ($aResults as $cSite) {
		$cSite = str_replace('/var/www/vhosts/', 'http://www.', $cSite);
		$cSite = str_replace('httpdocs/', '', $cSite);
		$cSite = str_replace('wp-config.php', '', $cSite);

		$rsMeta = get_meta_tags($cSite);

		if ($rsMeta['generator'] != 'WordPress ' . $fVersion) {
			$cMsg .= "**********************************************************************\n";
			$cMsg .= "WARNING!\n";
			$cMsg .= $rsMeta['generator'] . ' != WordPress ' . $fVersion . "\n";
			$cMsg .= "$cSite\n";
			$cMsg .= "**********************************************************************\n";
		} // ends
	} // ends

	if (!empty($cMsg)) {
		echo("$cMsg");
		mail('eric@ericnagel.com', 'WordPress needs update', $cMsg, "From: eric@ericnagel.com");
	} // ends

First, I check for the current version of WordPress. I have no better way to do this than pull the text off the WordPress download page. I’m sure there’s a better method, but after looking for almost 20 minutes, I gave up.

Then, I run the find command to get a list of all of my sites. This works nicely on my server:

/var/www/vhosts/domain01.com/httpdocs/wp-config.php
/var/www/vhosts/domain02.com/httpdocs/wp-config.php
/var/www/vhosts/domain03.com/httpdocs/wp-config.php
/var/www/vhosts/domain04.com/httpdocs/wp-config.php
/var/www/vhosts/domain05.com/httpdocs/wp-config.php
/var/www/vhosts/domain06.com/httpdocs/wp-config.php
/var/www/vhosts/domain07.tv/httpdocs/wp-config.php
/var/www/vhosts/domain08.us/httpdocs/blog/wp-config.php
/var/www/vhosts/domain09.com/httpdocs/blog/wp-config.php
/var/www/vhosts/domain10.com/httpdocs/wp-config.php
/var/www/vhosts/domain11.com/httpdocs/wp-config.php

Next, I hit each of the sites & get the meta “generator” tag. If the version doesn’t line up, then a warning is appended to a variable. At the end of the script, if the variable isn’t empty, then an email is sent to me.

The “find” command will throw errors if you don’t have permission to search a folder, so when you set-up the daily cron, suppress error messages:

/usr/bin/php -q /home/esnagel/wp-check/wp-check.php 2>/dev/null

It’s not the prettiest, but it gets the job done.

How To & Tips
Share this Story:
  • facebook
  • twitter
  • gplus

Comments(3)

  1. Eric Nagel
    2601 days ago

    WordPress was updated to version 2.8.5 the other day, and I’ve been getting my emails (because I haven’t upgraded yet)

    WARNING!
    WordPress 2.8.4 != WordPress 2.8.5

    Glad to see it’s working 😀

  2. AcquarioMarinho
    2135 days ago

    Have any way to check wordpress version just looking site?

    thanks

    • Eric Nagel
      2135 days ago

      Look at the “generator” meta tag. You’ll see something like:
      <meta name=”generator” content=”WordPress 3.0.4″/>

      Some themes may strip this out, but this is what I’m using for the script above.

Leave a comment

Comment