Themergency fooplugins.com

Check Minimum PHP and WordPress Versions

| 0 comments

This is day 6 of my WordPress Developer Advent Calendar.

While developing plugins for WordPress, you might need to check the configuration that is running the site. Specifically, I am talking about the version of PHP that is running on the server, and the version of WordPress that is currently running.

Check Minimum PHP Version

Perhaps you are using something that is only available in PHP 5.3, for example : anonymous functions. I have used this function many times in the past to check that the user is running a minimum version of PHP:

// check the version of PHP running on the server
function check_php_version($ver) {
    $php_version = phpversion();
    if (version_compare($php_version, $ver) < 0) {
        throw new Exception("This plugin requires at least version $ver of PHP. You are running an older version ($php_version). Please upgrade!");
    }
}

Then checking for PHP 5.3 would entail the following call:

check_php_version('5.3.0');

Check Minimum WordPress Version

Now perhaps you are using the new Heartbeat API that came around in WordPress v3.6, then you could use this function to check the version of WordPress:

// check the version of WP running
function check_wp_version($ver) {
    global $wp_version;
    if (version_compare($wp_version, $ver) < 0) {
        throw new Exception("This plugin requires at least version $ver of WordPress. You are running an older version ($php_version). Please upgrade!");
    }
}

Then checking for WordPress 3.6 you could call:

check_wp_version('3.6');

Just a word of warning with these functions. If the minimum versions are not found, then they throw errors, which will stop your plugin from running. If you would rather just warn the user, then you could alter the functions to rather return false.