If possible, how can installed plugins (meaning the files have been placed in wp-content/plugins directory) be activated from other plugins?
6 Answers
This is how I did it in some web apps:
function run_activate_plugin( $plugin ) {
$plugin = trim( $plugin );
$current = get_option( 'active_plugins' );
$plugin = plugin_basename( $plugin );
if ( !in_array( $plugin, $current ) ) {
$current[] = $plugin;
sort( $current );
do_action( 'activate_plugin', $plugin );
update_option( 'active_plugins', $current );
do_action( 'activate_' . $plugin );
do_action( 'activated_plugin', $plugin );
}
return null;
}
run_activate_plugin( 'akismet/akismet.php' );
-
1As Rarst wrote, this is not very "safe". You should make sure the plugins you activate this way are safe / will not break your site.– sorich87Commented Nov 14, 2010 at 15:46
-
12Not safe at all, even, but heck, if you're activating them programmatically you're supposed to know what you're doing. ;-) Commented Nov 14, 2010 at 19:45
-
3This, at least more recently, is reinventing the wheel. I have used the built-in
activate_plugin()
, and it seems to work the same.– JakeCommented Sep 18, 2013 at 18:21 -
2@SagiveSEO Its perfectly safe - its similar to how WP does it, except by requiring user input/approval and form nonces and checks for those nonces - just introduce necessary checks for initiating that function anywhere - so that it cannot be accessed from outside (from web etc), and it cannot be randomly called in any way.– unity100Commented Oct 24, 2018 at 1:38
-
1Just dropping a comment to agree with @unity100 and to disarm these "not safe" comments. This is a perfectly fine way to do. It's about where and how you will use it, that can make it unsafe. But that goes for just about any piece of code you copy from the internetz. Commented Apr 24, 2020 at 10:44
Plugin activation process is coded to work with WP admin interface. It performs some checks to prevent enabling plugins with errors (loading such on start might break WP).
It is handled by activate_plugin()
function (source) which is documented as unusable elsewhere.
So if you want to activate plugin by code the goal itself is relatively easy - to change active_plugins
option to include that plugin. But you will have to re-create related activation hooks from scratch and will risk breaking site by activating without sandbox step.
Plugins are stored in an array in the 'active_plugins' option. The array contains the file path to each plugin that is active.
To activate a plugin you need to determine what it's path will be, then pass that path to activate_plugin($plugin_path)
.
This is easier said than done though, and (at least in 2.9) the core code does not make it easy.
Before you can activate_plugin() you need to include the plugin.php file from wp-admin/includes/. You should also check to make sure your plugin isn't already active. The result looks something like this (YMMV):
// Define the new plugin you want to activate
$plugin_path = '/path/to/your/new/plugin.php';
// Get already-active plugins
$active_plugins = get_option('active_plugins');
// Make sure your plugin isn't active
if (isset($active_plugins[$plugin_path]))
return;
// Include the plugin.php file so you have access to the activate_plugin() function
require_once(ABSPATH .'/wp-admin/includes/plugin.php');
// Activate your plugin
activate_plugin($plugin_path);
I use this on production in WP 2.9 and have not had any major issues but in my testing it had very unexpected results with WPMU, so beware using this on network installs.
-
a long time passed but if you remember, what was unexpected result on this? Commented Aug 25, 2012 at 10:59
-
I completely don't remember. I think it just didn't work and I did things by hand because I didn't have too many MS sites. Commented Aug 28, 2012 at 17:48
-
how do I execute this? if i just execute it directly, get_option is not defined... Commented Aug 25, 2016 at 9:45
-
Add this in functions.php and it should work, though even then you should always put things in functions and hook them to "init" or "wp". Commented Aug 26, 2016 at 14:57
Wordpress provides a function for activating plugins, according to WP Codex
function plugin_activation( $plugin ) {
if( ! function_exists('activate_plugin') ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
if( ! is_plugin_active( $plugin ) ) {
activate_plugin( $plugin );
}
}
plugin_activation('akismet/akismet.php');
-
-
While posting just the code is okay for an answer, it may be hard to interpret for any new programmers that read your answer. This way, OP can have a better understanding on how your code works. Commented Oct 28, 2016 at 21:32
You can simply call the WordPress default function.
activate_plugin('akismet/akismet.php');
Please check below link for more detail.
If you know the names of the plugins, activate_plugin() function can be used to activate them. In the example below, I am using it with after_setup_theme hook, but you can change this to better fit your case.
In this example, I am activating the WooCommerce plugin:
function activate_mytheme_required_plugins() {
if (!is_plugin_active('woocommerce/woocommerce.php')) {
activate_plugin('woocommerce/woocommerce.php');
}
}
add_action('after_setup_theme', 'activate_mytheme_required_plugins');