Activate WordPress Plugin Using Code

  • Avinash
  • 2
  • Feb 21, 2012
  • Web Development

Recently I have written few articles/tips for wordpress. So here is one more article on this series. In this tutorial we will see how we can activate wordpress plugins programatically.

This would be useful when you don’t want some of your plugin to be disabled. Using below code your those plugins will always remain activated even if some one has deactivated form the admin panel. :)

How let’s have a look at below code block for the same. You just need to place below code in functions.php file of your active theme.

// Get currently activated plugins $current_plugin = get_option("active_plugins"); if(count($current_plugin)>0) { //Array of plugin which you want to remain active $always_active_plugin = array("plugin_x", "plugin_y", "plugin_z"); // Looping through active plugins foreach ($always_active_plugin as $plugin_name) { // If plugin is not active then add to list if(!in_array($plugin_name, $current_plugin)) { array_push($current_plugin ,$plugin_name); } } // Finally update plugins. update_option('active_plugins',$current_plugin ); }
// Get currently activated plugins
$current_plugin = get_option("active_plugins");

if(count($current_plugin)>0)
{
  //Array of plugin which you want to remain active
  $always_active_plugin = array("plugin_x", "plugin_y", "plugin_z");
 
  // Looping through active plugins
  foreach ($always_active_plugin as $plugin_name)
  {
    // If plugin is not active then add to list
    if(!in_array($plugin_name, $current_plugin))
    {
      array_push($current_plugin ,$plugin_name);
    }
  }
  // Finally update plugins.
  update_option('active_plugins',$current_plugin );
}

Hope this will be useful when you need this. Subscribe to our RSS Feed to get updates via email.

Related Posts

Written by Avinash

Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.

View all posts by:

  • Pingback: Activate WordPress Plugin Using Code | PHP | Syngu

  • Beau

    Some good code here. I would caution though that instead of just updating the WordPress options to include the new always used plugins a person should likely use the activate_plugin() function located in plugins.pho do the proper activation hook for the plugins run. I have not done much with this myself so take it with a grain of salt. Just something I am playing with right now.