Tuesday 13 September 2011

How to Activate or Deactivate wordpress plug-in programmatically?


Recently I had a problem with wordpress plug-in display at the dashboard. I searched on the web but did not find a proper solution. At that time I got a requirement to install one plug-in, I uploaded the plug-in from ftp and the uploaded plug-in is not displayed in the dashboard to activate.

For this I tried the following hook and done the activation of the plug-in.

function activate_plugin() {    
         // Absolute path to your specific plugin
$my_plugin = WP_PLUGIN_DIR.'/wp-email/wp-email.php';
 // Here wp-email is the plug-in which i want to activate. Replace it with the plug-in which you want to activate.
    // Check to see if plugin is already active
    if(is_plugin_active($my_plugin)) {
        // Deactivate plugin
        // Note that deactivate_plugins() will also take an
        // array of plugin paths as a parameter instead of
        // just a single string.
        deactivate_plugins($my_plugin);
    }
    else {
        // Activate plugin
        activate_plugin($my_plugin);
    }
}
toggle_plugin();

Add the above code to the functions.php of your current theme .Once the activation is done comment the code, because we are activating the plug-in if It is in deactivated state and we are deactivating the plug-in if it is active state. So if we did not remove the code the plug-in will be deactivated again.
If you don’t need to deactivation functionality just comment the following line and no need of commenting the whole function
        deactivate_plugins($my_plugin);
This code made my work easy that’s why I am sharing it , so it may be useful for someone.

No comments:

Post a Comment