Activate WordPress Plugins Automatically via a Function
December 26, 2014 0 Comments
You have probably seen it before, you install a theme or plugin and a message displayed if you want to activate some related plugins as well.
In many cases, this could be a plugin that is necessary for the theme / plugin process or, in other cases, you just want to market another plugin that you developed or that you have a joint venture agreement with.
This simple tutorial will guide you on how to activate a plugin that is already installed, it will not show you how to install a plugin that is not already installed (we will cover it in a future tutorial).
The idea behind this process is simple, WordPress stores all the active plugins’ list in the options table.
A special field is assigned to that list under the name activate_plugins .
So in order to activate any plugin that is already installed we just need to change this field value.
Before we start writing the simple code, we need to find the plugin file name as we are going to use the activation process.
A simple way to find the plugin’s file name is by using the code below. These few lines will add the plugin’s file name into the plugins page list, so you can use the activation code later (a plugin version of this code is attached to this tutorial, you can download it and activate it on your website):
/* Getting plugin file names */ add_filter( 'plugin_row_meta', 'wpinsiders_get_plugin_string', 10, 4 ); function wpinsiders_get_plugin_string( $plugin_meta, $plugin_file, $plugin_data, $status ) { /* echo plugin file string */ echo '<code>' . $plugin_file . '</code><br>'; return $plugin_meta; } Now that we know the plugin's name, we can use the following code inside our plugin or theme in order to activate the plugin/s: /* example on admin init, control about register_activation_hook() */ add_action( 'admin_init', 'wpinsiders_activate_plugins' ); // the exmple function function wpinsiders_activate_plugins() { if ( ! current_user_can('activate_plugins') ) wp_die(__('You do not have sufficient permissions to activate plugins for this site.')); $plugins = FALSE; $plugins = get_option('active_plugins'); // get active plugins if ( $plugins ) { // plugins to active $plugins_to_activate = array( 'hello.php', // Hello Dolly 'adminimize/adminimize.php', // Adminimize 'akismet/akismet.php' // Akismet ); foreach ( $plugins_to_activate as $plugin ) { if ( ! in_array( $plugin, $plugins ) ) { array_push( $plugins, $plugin ); update_option( 'active_plugins', $plugins ); } } } // end if $plugins }
As you can see in the above code, you can activate more than one plugin at once just by filling-in the right information inside the $plugin_to_activates array.
Important Notice:
Important aspect that you should know about this process is: When WordPress activates a plugin on the back-end using the activate command in the plugins’ page, it is activating it inside a “sandbox”. If the plugin is broken, your website will not be broken and you will get a message regarding the plugin’s error on WordPress admin back-end. However, when activating a plugin using a code, no sandbox is created and the plugin is activated in the live environment, so if the plugin that is activated has an error, it will break the website as well.
Examples for usage:
- The plugin needs some other plugins activated in order to work properly.
- You are creating a testing environment and you need to activate / deactivate several plugins at one.
- You want to make sure a plugin is active (for any reason that you can think of).
If you like this tutorial share using the social sharing buttons, if you want to add new information feel free to write in the comments section below.
No comments yet... Be the first to leave a reply!