Add Snippet To Project
Are you tired of manually activating plugins every time you install them on your WordPress site? Activating each plugin individually can be time-consuming and tedious, especially if you have multiple plugins to activate. Luckily, there is a solution: automatically activating plugins. In this article, we will show you how to automate the activation of plugins on your WordPress site, saving you time and effort.
function wpturbo_activate_plugins() {
$plugins = array(
'plugin1/plugin1.php',
'plugin2/plugin2.php',
'plugin3/plugin3.php'
);
foreach ($plugins as $plugin) {
$plugin_path = WP_PLUGIN_DIR . '/' . $plugin;
if (!is_plugin_active($plugin)) {
activate_plugin($plugin_path);
}
}
}
add_action('admin_init', 'wpturbo_activate_plugins');
The code snippet provided allows for the automatic activation of specific plugins in WordPress. This can be particularly useful when developing a theme or a plugin that requires certain plugins to be active for proper functionality.
The first part of the code snippet creates a new function named wpturbo_activate_plugins()
. This function is responsible for activating the desired plugins.
Next, we define an array called $plugins
that contains the slugs of the plugins we want to activate. The slugs should follow the format plugin-folder/plugin-file.php
. In this example, we have three plugins listed: plugin1, plugin2, and plugin3.
Using a foreach
loop, we iterate through each plugin in the $plugins
array. Inside the loop, we define a variable called $plugin_path
to store the full path of the plugin file. The WP_PLUGIN_DIR
constant is used to retrieve the directory where the plugins are located, and it is concatenated with the respective plugin slug.
Inside the loop, we check if the plugin is already active by using the is_plugin_active()
function. This function takes the plugin slug as a parameter and returns true
if the plugin is active or false
if it is not.
If the plugin is not active, we proceed to activate it using the activate_plugin()
function. This function takes the full path of the plugin file as a parameter and activates the plugin programmatically.
By hooking the wpturbo_activate_plugins()
function into the admin_init
action, we ensure that the function is executed whenever the admin area is initialized.
With this code snippet in place, the listed plugins will be automatically activated whenever the admin area is accessed, guaranteeing that the required functionality is available for the development process.