Created
January 28, 2024 00:22
-
-
Save brandonjp/f79d27dd65abd0291b1028e7bad8671c to your computer and use it in GitHub Desktop.
Deactivate Specific Plugins on Staging & Non-Production Sites [SnipSnip.pro] - https://snipsnip.pro/s/827
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Deactivate Specific Plugins on Staging & Non-Production Sites [SnipSnip.pro] - https://snipsnip.pro/s/827 | |
*/ | |
if (!class_exists('Deactivate_Plugins_On_Staging')) { | |
/** | |
* Selectively deactivates plugins on staging and similar environments. | |
*/ | |
class Deactivate_Plugins_On_Staging { | |
private $stagingKeywords = ['staging', 'dev', 'local', 'test', 'sandbox', 'demo']; | |
private $forceDeactivations = false; // Set to true to deactivate regardless of environment detection. | |
// List the slugs of plugins you want to deactivate | |
const PLUGINS_TO_DEACTIVATE_ON_STAGING = [ | |
'query-monitor', | |
'create-block-theme', | |
'ai-engine*' | |
]; | |
public function __construct() { | |
// Apply deactivations if forced or if the site is detected as a staging site. | |
if ($this->forceDeactivations || $this->isStagingSite()) { | |
add_filter('option_active_plugins', [$this, 'deactivatePluginsOnStaging']); | |
} | |
} | |
/** | |
* Checks if the current site URL matches any of the specified staging environment keywords. | |
* | |
* @return bool True if a matching keyword is found, indicating a staging environment. | |
*/ | |
private function isStagingSite() { | |
$currentUrl = strtolower(get_option('siteurl')); | |
foreach ($this->stagingKeywords as $keyword) { | |
if (strpos($currentUrl, $keyword) !== false) { | |
return true; | |
} | |
} | |
return false; | |
} | |
/** | |
* Deactivates the specified plugins by filtering them out from the list of active plugins. | |
* | |
* This method is applied in staging environments only. | |
* | |
* @param array $activePlugins The list of currently active plugins' file paths. | |
* @return array The modified list with specific plugins deactivated. | |
*/ | |
public function deactivatePluginsOnStaging($activePlugins) { | |
foreach (self::PLUGINS_TO_DEACTIVATE_ON_STAGING as $pluginPattern) { | |
$pattern = strtolower($pluginPattern); | |
if (strpos($pattern, '*') !== false) { | |
$pattern = str_replace('*', '.*', $pattern); | |
$activePlugins = array_filter($activePlugins, function($plugin) use ($pattern) { | |
// Extract plugin slug from the path for comparison | |
$pluginSlug = strtolower(substr($plugin, 0, strpos($plugin, '/'))); | |
// Match against pattern (with wildcard support) | |
return !preg_match('/^' . $pattern . '$/', $pluginSlug); | |
}); | |
} else { | |
// Handle direct plugin slug match (no wildcards) | |
$pluginPath = $pattern; // Direct matching of the plugin slug to path | |
$key = array_search("$pluginPath/$pluginPath.php", $activePlugins); | |
if ($key !== false) { | |
unset($activePlugins[$key]); | |
} | |
} | |
} | |
return array_values($activePlugins); // Ensure the array is correctly indexed after modifications | |
} | |
} | |
new Deactivate_Plugins_On_Staging(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This "Deactivate_Plugins_On_Staging" script selectively deactivates specified plugins on your staging and non-production environments based on the site URL. Ideal for disabling plugins that are only necessary on your live site, such as Jetpack, analytics, or auto-social-posting plugins. Specify plugins to deactivate by adding their slugs to the
PLUGINS_TO_DEACTIVATE_ON_STAGING
array (you can use * wildcard matches). By default the script will assume this is a Life/Production site unless the site domain includes keywords like 'dev', 'staging', 'test', etc., which can be customized in the$stagingKeywords
array. For testing the script, you can use$forceDeactivations
set totrue
to make sure the deactivation behavior is working for the plugins you want. For more info, visit here: https://snipsnip.pro/s/827