Skip to content

Instantly share code, notes, and snippets.

@mattneal-stafflink
Created November 14, 2024 23:29
Show Gist options
  • Save mattneal-stafflink/f808ea5d6290286e04e7d57c925eacb8 to your computer and use it in GitHub Desktop.
Save mattneal-stafflink/f808ea5d6290286e04e7d57c925eacb8 to your computer and use it in GitHub Desktop.
Force active/inactive plugins based on your WP_ENV.
<?php
/*
Plugin Name: ENV Plugin Manager
Description: Enforces activation and deactivation of specific plugins based on environment. Add your plugins to the appropriate environment array in this file.
Version: 1.1
Author: Matthew Neal
Author URI: https://realcoder.com.au/
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Get the current environment.
$env = defined( 'WP_ENV' ) ? WP_ENV : 'production';
// Array of plugins to activate per environment.
$plugins_to_activate = array(
'local' => array(
'plugin-directory/plugin-file.php',
'another-plugin/another-plugin-file.php',
// Add more plugins to activate on local.
),
'staging' => array(
'plugin-directory/plugin-file.php',
// Add more plugins to activate on staging.
),
'production' => array(
'plugin-directory/plugin-file.php',
// Add more plugins to activate on production.
),
);
// Array of plugins to deactivate per environment.
$plugins_to_deactivate = array(
'local' => array(
'plugin-to-deactivate-on-local/plugin-file.php',
// Add more plugins to deactivate on local.
),
'staging' => array(
'plugin-to-deactivate-on-staging/plugin-file.php',
// Add more plugins to deactivate on staging.
),
'production' => array(
'plugin-to-deactivate-on-production/plugin-file.php',
// Add more plugins to deactivate on production.
),
);
/**
* Manages the activation and deactivation of plugins based on environment.
*
* @param array $plugins The array of active plugins.
* @return array Modified array of active plugins.
*/
function manage_plugins( $plugins ) {
global $plugins_to_activate, $plugins_to_deactivate, $env;
// Ensure the environment arrays are defined.
$activate_plugins = isset( $plugins_to_activate[ $env ] ) ? $plugins_to_activate[ $env ] : array();
$deactivate_plugins = isset( $plugins_to_deactivate[ $env ] ) ? $plugins_to_deactivate[ $env ] : array();
// Activate plugins specific to the current environment.
foreach ( $activate_plugins as $plugin ) {
if ( ! in_array( $plugin, $plugins, true ) && file_exists( WP_PLUGIN_DIR . '/' . $plugin ) ) {
$plugins[] = $plugin;
}
}
// Deactivate plugins specific to the current environment.
foreach ( $deactivate_plugins as $plugin ) {
$key = array_search( $plugin, $plugins, true );
if ( false !== $key ) {
unset( $plugins[ $key ] );
}
}
// If a plugin is both in activate and deactivate lists, deactivate it.
// This is already handled because we deactivate after activation.
// Reindex array to maintain sequential keys.
$plugins = array_values( $plugins );
return $plugins;
}
// Hook into the 'option_active_plugins' filter to modify the active plugins list.
add_filter( 'option_active_plugins', 'manage_plugins' );
// For multisite installations, also hook into 'site_option_active_sitewide_plugins'.
if ( is_multisite() ) {
/**
* Manages the activation and deactivation of network-activated plugins based on environment.
*
* @param array $plugins The array of active network-wide plugins.
* @return array Modified array of active network-wide plugins.
*/
function manage_network_plugins( $plugins ) {
global $plugins_to_activate, $plugins_to_deactivate, $env;
// Ensure the environment arrays are defined.
$activate_plugins = isset( $plugins_to_activate[ $env ] ) ? $plugins_to_activate[ $env ] : array();
$deactivate_plugins = isset( $plugins_to_deactivate[ $env ] ) ? $plugins_to_deactivate[ $env ] : array();
// Network-activate plugins specific to the current environment.
foreach ( $activate_plugins as $plugin ) {
if ( ! isset( $plugins[ $plugin ] ) && file_exists( WP_PLUGIN_DIR . '/' . $plugin ) ) {
$plugins[ $plugin ] = time();
}
}
// Network-deactivate plugins specific to the current environment.
foreach ( $deactivate_plugins as $plugin ) {
if ( isset( $plugins[ $plugin ] ) ) {
unset( $plugins[ $plugin ] );
}
}
return $plugins;
}
add_filter( 'site_option_active_sitewide_plugins', 'manage_network_plugins' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment