Displaying a message when plug-in is deactivated

I’m using the following code to deactivate my WordPress Plugin if the requirements are not met

    public function activate() {
        if (!$this->check_requirements()) {
            deactivate_plugins(plugin_basename(__FILE__));
            wp_redirect(admin_url('plugins.php'));
            exit;
        }
    }

The function activate() is called when the plug-in is activated. I want to add a message to the user explaining what happened. Is there a way to do that?

1 Answer
1

Actually, there’s a better way.

All of my plugins require PHP5. Until recently, that wasn’t a requirement in WordPress, so a lot of people tried to install my plugins on systems missing vital PHP functions. I added some checks to my plugin to make sure it would work and, if not, display a message.

But I keep the plugin active. It just doesn’t function. That way, the message sits there until either the user takes action to update their server or deactivates my plugin.

Here’s the gist of what I do …

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

if ( version_compare( PHP_VERSION, '5.0', '<' ) ) {
    add_action('admin_notices', '_my_plugin_php_warning');
} else {
    require_once( 'lib/class.my_plugin_core_functions.php' );
}

I put this code in the main PHP file for my plugin. Then, whenever the site loads, my plugin checks to make sure the site has the right version of PHP and either loads the rest of its required files (the require_once() call) or adds a notice to the admin section (the add_action() call).

Leave a Comment