Disable Message which Appears when Activating WordPress Plugin

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' );

Related posts

Leave a Reply

7 comments

  1. You could just unset the $_GET variable which triggers the message:

    if ( version_compare(phpversion(), $numPHPver, "<" ) ) {
        echo '<div class="error"><p>' . $strMsg . '</p></div>';
        deactivate_plugins( $plugin );
        unset($_GET['activate']);
    }
    

    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.

    function my_activation_hook() {
        $required_php_version = '5.1.2';
    
        if (version_compare(PHP_VERSION, $required_php_version, '<')) { 
            $plugin_data = get_plugin_data(__FILE__, false);
            $message = $plugin_data['Name'] . ' ' . __('requires PHP')  . ' ' . $required_php_version . __(' or higher. Your PHP version is ') . phpversion() . '.';
            echo "<p>{$message}</p>";
            exit;
        } 
    } 
    register_activation_hook(__FILE__, 'my_activation_hook');
    
  2. 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 the register_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:

    <?php
    /**
     * Plugin Name: My Plugin
     * Description: A sample plugin activation call without generating "Plugin activated."
     *     message if requirements aren't met.
     * Author: Slicktrick
     * Version: 1.0.0
     * Requires at least: 4.0
     */
    
    add_action( 'admin_init', 'check_requirements_for_my_plugin' );
    add_action( 'admin_notices', 'show_notices_for_my_plugin' );
    
    function check_requirements_for_my_plugin() {
        // Check that we are using PHP 5.1.2 or greater.
        // Modify this to use whatever checks you need.
        $phpversion = '5.1.2';
        if ( version_compare(phpversion(), $phpversion, "<" ) ) {
            // deactivate and remove flag to activate plugin
            deactivate_plugins( plugin_basename( __FILE__ ) );
            if ( isset( $_GET['activate'] ) ) {
                unset( $_GET['activate'] );
            }
    
            // Create an error message and store it as a transient
            // Transient is set to automatically expire after 120 seconds
            // This prevents the message from lingering indefinitely
            $error_message = "PHP version $phpversion required. My Plugin was not activated.";
            set_transient( 'my_plugin_activation_error_message', $error_message, 120 );
        } else {
            // Here's the trick, we register our activation hook
            // now that we know the requirements are met!
            register_activation_hook( __FILE__, 'activate_my_plugin' );
        }
    }
    
    function show_notices_for_my_plugin() {
        // We attempt to fetch our transient that we stored earlier.
        // If the transient has expired, we'll get back a boolean false
        $message = get_transient( 'my_plugin_activation_error_message' );
    
        if ( ! empty( $message ) ) {
            echo "<div class='notice notice-error is-dismissible'>
                <p>$message</p>
            </div>";
        }
    }
    
    function activate_my_plugin() {
        // Add activation code here
    }
    
    // Add the rest of your plugin code here
    

    The check_requirements_for_my_plugin() function will run every time admin_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 our check_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:

    // at the top of the file
    add_action( 'admin_notices', 'show_notices_for_my_plugin' );
    
    // from the check_requirements_for_my_plugin() function
    $error_message = "PHP version $phpversion required. My Plugin was not activated.";
    set_transient( 'my_plugin_activation_error_message', $message, 120 );
    

    References:

    1. WordPress Codex – Admin Notices
    2. WordPress Codex – Transients API

    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:

    add_action( 'plugins_loaded', 'check_requirements_for_my_plugin' );
    
    function check_requirements_for_my_plugin() {
        // Do whatever checking needed here
        if ( ! $requirements_met ) {
            add_action( 'admin_notices', 'show_requirements_notice_for_my_plugin' );
            return; // Exit your function
        }
        // Register all other hooks here since you know requirements are
        // met if you reach this point
        add_action( 'admin_init', 'maybe_update_my_plugin' );
    }
    
    function show_requirements_notice_for_my_plugin() {
        // I'd expand this to add a list of needed requirements
        echo '<div class="notice notice-error">'
            . "My-Plugin functionality is disabled because the requirements are not met."
            . '</div>';
    }
    
    function maybe_update_my_plugin() {
        // Check to see if update/installation tasks need to be performed
    }
    

    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.

  3. function _my_plugin_php_warning() {
        echo '<div id="message" class="error">';
        echo '  <p>My Plugin requires at least PHP 5.  Your system is running version ' . PHP_VERSION . ', which is not compatible!</p>';
        echo '</div>';
    }
    
    function deactivate_plugin_conditional() {
    
        if ( version_compare( PHP_VERSION, '5.2', '<' ) ) {
            $plugin = plugin_basename(__FILE__);
    
            if ( is_plugin_active($plugin) ) {
                deactivate_plugins($plugin);    
            }   
            add_action('admin_notices', '_my_plugin_php_warning');
    
        } else {
            //Load classes
        }
    }
    
    add_action( 'admin_init', 'deactivate_plugin_conditional' );
    

    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.

  4. 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.

  5. 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:

    PHP Notice: Undefined variable: wp_version (…)

    The problem was solved when I replaced $wp_version with get_bloginfo('version')