Activate WordPress Plugin Using Code

By Avinash

February 21, 2012Web Development2 Comments

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.

Share This Article

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