I’m using the code below to deactivate the plugin itself when the user’s PHP version is not sufficient. One problem is that the yellow message box appears saying that the plugin is activated although it is successfully denied by this function. So is there a way not to display the yellow message?
function Plugin_Requirements() {
global $wp_version;
$plugin = plugin_basename( __FILE__ );
$plugin_data = get_plugin_data( __FILE__, false );
$numPHPver='5.1.2';
$strMsg .= $plugin_data['Name'] . ': ' . __('requires PHP') . ' ' . $numPHPver . __(' or higher. Your PHP version is : ') . phpversion() . __('. Deactivating the plugin.') . '<br />';
if ( version_compare(phpversion(), $numPHPver, "<" ) ) {
echo '<div class="error"><p>' . $strMsg . '</p></div>';
deactivate_plugins( $plugin );
}
}
add_action( 'admin_init', 'Plugin_Requirements' );
You could just unset the
$_GET
variable which triggers the message:I think a better approach is to not allow your plugin to be activated, by exiting from (or throwing an error in) it’s activation hook. WordPress will display an error message stating the plugin could not be activated due to a fatal error, but that seems appropriate here.
Getting this just right is a bit tricky. RichardM’s answer is very close, but here is what I have found works without showing either a fatal error or having to use
wp_die()
. The trick is to call theregister_activation_hook()
after you have checked the requirements for your plugin. If you don’t do this, then WordPress happily activates the plugin and generates the “Plugin activated.” message before you have checked your requirements.I also like to use transients for my messages so I can display my error messages at any time. It’s more useful when you have multiple messages that you need to communicate to your user.
Sample code:
The
check_requirements_for_my_plugin()
function will run every timeadmin_init
is executed. While this might seem like a redundant check once the plugin is activated, think about what might happen if someone changes hosting providers or moves to a different server. The new server environment may not support your plugin, and if the WordPress database was copied over directly (most likely), the user could end up with all kinds of errors and broken functionality. So calling ourcheck_requirements_for_my_plugin()
function every time is a necessary evil.If you don’t want to show any message to the user when your plugin fails to activate (why would you want to leave your user in the dark about why your plugin didn’t activate???), then you can remove the following three lines of code plus the entire
show_notices_for_my_plugin()
function:References:
Update
Based on this answer and this comment from a WordPress Lead Developer it seems the better route to take is to leave your plugin activated but disable all functionality. You can then use other checks once the requirements are met to do any installation tasks you need to do. This can be easily done with with the following example code:
This method allows you to keep notices in front of the user so they can take action on it. My original answer would show a notice when they try to activate the plugin, but the notice disappears as soon as a new page is loaded since the plugin is no longer active at that point.
This will only activate the plugin when no error occured. If the plugin is allready active it wil be disabled. Hopefully this works for you. Please let me know what’s the result.
get_plugin_data()
function will only work in WordPress admin, instead on using get_plugin_data() you can define constants in your plugin for plugin name and plugin version, take a look at this thread
http://wordpress.org/support/topic/php-fatal-error-call-to-undefined-function-get_plugin_data
If you don’t want the message to appear at all, go to the php page and add error_reporting(0) immediately after the opening php tag at the top of the page.
I had a similar problem involving
register_activation_hook()
and the global variable $wp_version: every time my plugin was activated or deactivated, I got the following message in the debug.log file:The problem was solved when I replaced $wp_version with
get_bloginfo('version')
It seems it is currently not possible to do it as a part of plugin.