WordPress function to check if a minimum set of requirements are met
Combining a couple of older functions, adding in some further automation and generally bringing them to a higher code quality, I’ve now created a new WordPress function for checking minimum requirements for a plugin.
The idea is, you add this to your plugin and, when run, it ensures that certain checks are passed. If not, a message is displayed and the plugin is deactivated.
Here’s the code – instructions are after…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if ( version_compare( get_bloginfo( 'version' ), $minimum_wp, '<' ) && ( false == $exit ) ) { // This second check is so that a WP check isn't performed on a fork (which may be using different version numbers anyway).
$exit = true;
/* translators: 1: minimum WordPress version, 2: the current WordPress version. */
$reasons .= '<li>' . sprintf( __( 'This plugin requires a minimum WordPress version of %1$s. This site is using WordPress %2$s.', 'your-text-domain' ), $minimum_wp, get_bloginfo( 'version' ) ) . '</li>';
}
// Check for PHP requirements.
if ( version_compare( PHP_VERSION, $minimum_php, '<' ) ) {
$exit = true;
/* translators: 1: minimum PHP version, 2: the current PHP version. */
$reasons .= '<li>' . sprintf( __( 'This plugin requires a minimum PHP level of %1$s. This site is using PHP %2$s.', 'your-text-domain' ), $minimum_php, PHP_VERSION ) . '</li>';
}
// If a reason was found, deactivate the plugin and output a message.
$message .= '<ul>' . $reasons . '</ul><p>' . sprintf( __( 'The author of %1$s will not provide any support until the above are resolved.', 'your-text-domain' ), $name ) . '</p>';
Make sure the PHP level meets the minimum requirement
Make sure the WordPress version meets the minimum requirement
Make sure it’s not running on a forked version of a WordPress (checks for CalmPress and ClassicPress)
1 and 2 minimum requirements are based on the meta in the README, which you’ll need to make sure you specify. This is an example of what you need to add to the header…
Requires at least: 4.6
Requires PHP: 7.4
I should also note that both of these are not needed – recent versions of WordPress checks for this and prevents the plugin from being enabled. However, both are presented here of demonstrations of what you can do, particularly with looking up the meta data.
They also serve as a secondary lock, if the core solution doesn’t work.
You will to change the text domain, which is currently set to your-text-domain.
If the function is added to your base plugin file then no further changes are required, otherwise, you’ll need to add the basename to line 13 – this is in the format of {plugin folder}/{plugin name}.php.
If you have any queries, please ask on the Gist – I’m closing comments on this post, due to the abuse I get whenever I mention ClassicPress.