Web Development

Activate WordPress Plugin Using Code

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.

[cc lang=”php”]
// 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 );
}
[/cc]

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

Shares:
  • […] This would be useful when you don’t want some of your plugin to be disabled    PHP Read the original post on DZone… […]

    Reply
  • Beau
    Beau
    November 6, 2012 at 4:48 am

    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.

    Reply
  • Beau
    Beau
    December 20, 2019 at 2:53 pm

    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.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *