So, if a plugin should not ever be installed on an older version of WordPress, whats the best way to go about it? What I normally do is something like this:
if ( ! function_exists( 'get_post_format' ) ) {
$error_handler = set_error_handler( 'my_plugin_die' );
trigger_error( '', E_USER_ERROR );
}
function my_plugin_die( $errno, $errstr, $errfile, $errline ) {
global $wp_version;
exit( 'This plugin requires WordPress version 3.0 or greater. You are currently using version ' . $wp_version . '. Please <a target="_top" href="' . esc_url( admin_url( 'update-core.php' ) ) . '">upgrade to the latest version of WordPress</a> before installing this plugin.' );
}
I place this straight in the plugin file – outside of any class. It seems to work well in all of my testing, but seems rather harsh. Is there any chance of a plugin having this code will ever be activated?
How to you deal with situations like this? There seems to be absolutely no documentation on this kind of thing.
I think you’re taking the right approach: version-checking and die. The only thing I might recommend would be to hook it into the Plugin activation hook.
Out of curiosity, though: why aren’t you using
wp_die()
(Codex ref)?As a side note: I would love to see the Theme and Plugin repositories implement some sort of UI similar to AMO, that indicates whether a given Theme/Plugin is compatible with the user’s current WordPress version. Plugins have a
Requires:
WordPress-version header tag that would work sufficiently for this functionality, and I would think that Themes could implement the same, just as easily.p.s. I would strongly recommend against using the “Compatibility rating”; that functionality is beyond broken.