Ace Goulet

Automatically Deactivate Plugins

This is specific to environments where you have production and staging environments with services to sync the two.

Sometimes you run into a situation where you want a plugin running on production, but not staging. The problem is that that auto sync from prod -> stag will activate the plugin on stag and the extra step to go over and disable it is often overlooked. Enter the deactivate_plugins() wp function. In this example, I hooked into admin_init and added a function that first checks if the environment is not production, then checks if the plugin is active, then deactivates it if it is.

Here’s an example if for some reason you wanted to disable akisment on staging:

$env_domain = $_SERVER['SERVER_NAME'];
if(strrpos($env_domain , 'staging.wpengine') !== false){
     $site_env = 'staging';
}
else if (strrpos($env_domain , 'localhost') !== false){
     $site_env = 'dev';
}
else {
     $site_env = 'production';
}

if($site_env !=='production'){
     function kill_plugins(){
          if(is_plugin_active( plugin_basename( '/akismet/akismet.php' ) )){
               deactivate_plugins( plugin_basename( '/akismet/akismet.php' ) );
          }
     }
     add_action( 'admin_init', 'kill_plugins' );
}
Exit mobile version