Skip to content

Instantly share code, notes, and snippets.

@brandonjp
Last active January 28, 2024 00:21
Show Gist options
  • Save brandonjp/83f06fd895e584c470b1a735453357a9 to your computer and use it in GitHub Desktop.
Save brandonjp/83f06fd895e584c470b1a735453357a9 to your computer and use it in GitHub Desktop.
Deactivate Specific Plugins on Production Sites [SnipSnip.pro] - https://snipsnip.pro/s/825
/**
* Deactivate Specific Plugins on Production Sites [SnipSnip.pro] - https://snipsnip.pro/s/825
*/
if (!class_exists('Deactivate_Plugins_On_Production')) {
/**
* Selectively deactivates plugins on production environments.
*/
class Deactivate_Plugins_On_Production {
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 on production
const PLUGINS_TO_DEACTIVATE_ON_PRODUCTION = [
'query-monitor',
'create-block-theme',
'ai-engine*'
];
public function __construct() {
// Apply deactivations if forced or if the site is detected as a production site.
if ($this->forceDeactivations || $this->isProductionSite()) {
add_filter('option_active_plugins', [$this, 'deactivatePluginsOnProduction']);
}
}
/**
* Determines if the current site environment is production by not matching any of the specified staging keywords.
*
* @return bool True if the environment is considered production.
*/
private function isProductionSite() {
$currentUrl = strtolower(get_option('siteurl'));
foreach ($this->stagingKeywords as $keyword) {
if (strpos($currentUrl, $keyword) !== false) {
return false;
}
}
// If none of the staging keywords are found, it's considered a production environment.
return true;
}
/**
* Deactivates the specified plugins by filtering them out from the list of active plugins in production environment.
*
* @param array $activePlugins The list of currently active plugins' file paths.
* @return array The modified list with specific plugins deactivated on production.
*/
public function deactivatePluginsOnProduction($activePlugins) {
foreach (self::PLUGINS_TO_DEACTIVATE_ON_PRODUCTION as $pluginPattern) {
$pattern = strtolower($pluginPattern);
if (strpos($pattern, '*') !== false) {
$pattern = str_replace('*', '.*', $pattern);
$activePlugins = array_filter($activePlugins, function($plugin) use ($pattern) {
$pluginSlug = strtolower(substr($plugin, 0, strpos($plugin, '/')));
return !preg_match('/^' . $pattern . '$/', $pluginSlug);
});
} else {
$pluginPath = $pattern;
$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_Production();
}
@brandonjp
Copy link
Author

This "Deactivate_Plugins_On_Production" script selectively deactivates specified plugins on your production environments based on the site URL. Ideal for disabling plugins that are only necessary on your staging or development sites, such as Query Monitor or Create Block Theme or test utilities or development tools. Specify plugins to deactivate by adding their slugs to the PLUGINS_TO_DEACTIVATE_ON_PRODUCTION array (you can use * wildcard matches). By default, the script will assume this is a Staging/Non-Production site unless the site domain lacks keywords like 'dev', 'staging', 'test', etc., which are checked against in the $stagingKeywords array. For testing the script, you can use $forceDeactivations set to true to ensure the deactivation behavior is working for the plugins you want.  For more info, visit here: https://snipsnip.pro/s/825

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment