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?
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 …
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 (theadd_action()
call).