2

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?

2
  • @kaiser Sorry, but if there is no response that could be picked as answer then doing so will just make the StackExchange a worse place.
    – Omar Abid
    Commented Oct 26, 2011 at 21:39
  • If your other questions don't have acceptable answers ... go back and modify your questions to provide more information. It might just be that your questions aren't clear enough.
    – EAMann
    Commented Oct 26, 2011 at 21:45

1 Answer 1

3

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

4
  • That's exactly what I'm doing. I'm afraid that activating the plug-in will make the users confused.
    – Omar Abid
    Commented Oct 26, 2011 at 21:40
  • How would it cause confusion? From a user's perspective the exact same thing is happening - they're clicking "Activate Plugin" and seeing a "you can't do this because of X" message. Whether it stays active of not is inconsequential so long as you disable any functionality that might break on the site.
    – EAMann
    Commented Oct 26, 2011 at 21:46
  • Also, this is the only way you can make sure the admin notice actually displays on the site. If the plugin is inactive, you won't be able to hook into the admin_notices action.
    – EAMann
    Commented Oct 26, 2011 at 21:47
  • That's exactly what I was asking for, if there is a way to hook into the admin_notices just after I disable the plug-in. Thanks for the suggestion.
    – Omar Abid
    Commented Oct 26, 2011 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.