Maintaining different environments for a WordPress site—such as development, staging, and production—can often require specific plugins to be active in one environment and not in others. The following PHP code snippet simplifies the process of enabling and disabling plugins based on the environment type, right from the WordPress admin dashboard.
The Code
<?php
function env_plugins_notice() {
$all_plugins = get_plugins();
$local_plugins = ['Query Monitor' => 'query-monitor/query-monitor.php'];
$prod_plugins = [
'WP Rocket' => 'wp-rocket/wp-rocket.php',
'Google Tag Manager for WordPress' => 'duracelltomi-google-tag-manager/duracelltomi-google-tag-manager-for-wordpress.php'
];
$to_enable = [];
$to_disable = [];
$isProd = wp_get_environment_type() === 'production';
if (is_admin()) {
foreach ($local_plugins as $plugin_name => $plugin_directory) {
if (array_key_exists($plugin_directory, $all_plugins)) {
if ($isProd) {
if (is_plugin_active($plugin_directory)) {
$to_disable[$plugin_name] = $plugin_directory;
}
} else {
if (!is_plugin_active($plugin_directory)) {
$to_enable[$plugin_name] = $plugin_directory;
}
}
}
}
foreach ($prod_plugins as $plugin_name => $plugin_directory) {
if (array_key_exists($plugin_directory, $all_plugins)) {
if ($isProd) {
if (!is_plugin_active($plugin_directory)) {
$to_enable[$plugin_name] = $plugin_directory;
}
} else {
if (is_plugin_active($plugin_directory)) {
$to_disable[$plugin_name] = $plugin_directory;
}
}
}
}
if (!empty($to_enable) || !empty($to_disable)) {
echo '<div class="notice notice-error is-dismissible">';
echo sprintf(__('<p><strong>Plugin activation/deactivation (Current environment: %s)</strong><br/>'), wp_get_environment_type());
if (!empty($to_enable)) {
echo sprintf(__('The following plugins will be enabled: %s<br/>'), implode(', ', array_keys($to_enable)));
}
if (!empty($to_disable)) {
echo sprintf(__('The following plugins will be disabled: %s'), implode(', ', array_keys($to_disable)));
}
echo '</p>';
$action_url = admin_url('plugins.php');
$action_url = add_query_arg(['action' => 'setup_plugins_env'], $action_url);
echo sprintf(__('<p><a href="%s" class="button-primary">Enable / Disable plugins for %s environment</a></p>'), esc_url($action_url), wp_get_environment_type());
echo sprintf(__('<p><em>You\'re not on the %s server? Check the environment configured in wp-config.php.</em></p>'), wp_get_environment_type());
echo '</div>';
}
global $pagenow;
if (
isset($_GET['action'])
&& $_GET['action'] == 'setup_plugins_env'
&& $pagenow == 'plugins.php'
) {
if (!empty($to_enable)) {
activate_plugins($to_enable);
}
if (!empty($to_disable)) {
deactivate_plugins($to_disable);
}
wp_redirect(admin_url($pagenow));
}
}
}
add_action('admin_notices', 'env_plugins_notice');
<?php
function env_plugins_notice() {
$all_plugins = get_plugins();
$local_plugins = ['Query Monitor' => 'query-monitor/query-monitor.php'];
$prod_plugins = [
'WP Rocket' => 'wp-rocket/wp-rocket.php',
'Google Tag Manager for WordPress' => 'duracelltomi-google-tag-manager/duracelltomi-google-tag-manager-for-wordpress.php'
];
$to_enable = [];
$to_disable = [];
$isProd = wp_get_environment_type() === 'production';
if (is_admin()) {
foreach ($local_plugins as $plugin_name => $plugin_directory) {
if (array_key_exists($plugin_directory, $all_plugins)) {
if ($isProd) {
if (is_plugin_active($plugin_directory)) {
$to_disable[$plugin_name] = $plugin_directory;
}
} else {
if (!is_plugin_active($plugin_directory)) {
$to_enable[$plugin_name] = $plugin_directory;
}
}
}
}
foreach ($prod_plugins as $plugin_name => $plugin_directory) {
if (array_key_exists($plugin_directory, $all_plugins)) {
if ($isProd) {
if (!is_plugin_active($plugin_directory)) {
$to_enable[$plugin_name] = $plugin_directory;
}
} else {
if (is_plugin_active($plugin_directory)) {
$to_disable[$plugin_name] = $plugin_directory;
}
}
}
}
if (!empty($to_enable) || !empty($to_disable)) {
echo '<div class="notice notice-error is-dismissible">';
echo sprintf(__('<p><strong>Plugin activation/deactivation (Current environment: %s)</strong><br/>'), wp_get_environment_type());
if (!empty($to_enable)) {
echo sprintf(__('The following plugins will be enabled: %s<br/>'), implode(', ', array_keys($to_enable)));
}
if (!empty($to_disable)) {
echo sprintf(__('The following plugins will be disabled: %s'), implode(', ', array_keys($to_disable)));
}
echo '</p>';
$action_url = admin_url('plugins.php');
$action_url = add_query_arg(['action' => 'setup_plugins_env'], $action_url);
echo sprintf(__('<p><a href="%s" class="button-primary">Enable / Disable plugins for %s environment</a></p>'), esc_url($action_url), wp_get_environment_type());
echo sprintf(__('<p><em>You\'re not on the %s server? Check the environment configured in wp-config.php.</em></p>'), wp_get_environment_type());
echo '</div>';
}
global $pagenow;
if (
isset($_GET['action'])
&& $_GET['action'] == 'setup_plugins_env'
&& $pagenow == 'plugins.php'
) {
if (!empty($to_enable)) {
activate_plugins($to_enable);
}
if (!empty($to_disable)) {
deactivate_plugins($to_disable);
}
wp_redirect(admin_url($pagenow));
}
}
}
add_action('admin_notices', 'env_plugins_notice');
The function env_plugins_notice is hooked into admin_notices to execute within the admin dashboard. It uses wp_get_environment_type() to determine the current environment and compares the active plugins against a predefined list suitable for local development or production.
How It Works
- The code defines two arrays:
$local_pluginsfor plugins intended for non-production environments, and$prod_pluginsfor those meant for production. - It checks the current environment and prepares lists of plugins to enable or disable accordingly.
- An admin notice is displayed, informing the user which plugins will be activated or deactivated based on the current environment.
- An action link is provided within the notice, allowing the user to apply the changes with a single click.
Implementation
To implement this functionality, add the provided code snippet to your theme’s functions.php file or a custom functionality plugin. This will enable the environment-based plugin management without the need to manually activate or deactivate plugins when moving between environments.
Benefits
- Environment-Specific Control: Ensures that only the appropriate plugins are active in each environment.
- User-Friendly Interface: Provides clear notifications and actions within the WordPress admin.
- Error Reduction: Minimizes the risk of human error by automating plugin management.

Leave a Reply