AI Verified

Name

Disable Specific Plugins on Production Environments [SnipSnip.pro]

About

<p>Disables the specified plugins on non-production environments (based on the site URL). You can specific which plugins you want deactivated by placing the plugin's slug in the `PLUGINS_TO_DEACTIVATE` array. By default it will assume this is a production site, unless the URL contains dev, staging, test, etc. (you can add your own staging values to the `$envKeywords` array). And if you need to test it, you can change `$alwaysRunDeactivations` to true and it will skip the environment check.  For more info, visit here: <a href="https://snipsnip.pro/s/825" target="_blank" rel="noopener">https://snipsnip.pro/s/825</a></p>

Language

PHP

Rating

Voted: 0 by 0 user(s)

How to Setup Snippet

For me, this was used to solve a critical error that kept happening when a particular plugin was active on our Production site. If you're just using it for performance or as a reminder to deactivate, then you could set this to Admin Only and just have it run when you login to the WP Admin. And if you need the reverse functionality (having plugins deactivated everywhere except production) then see here: https://snipsnip.pro/s/827

Link for further information:

The author has provided the following URL that may be helpful to setting up or using this snippet:

https://snipsnip.pro/s/825

Codevault

brandonjp

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.4

Our AI bot has checked this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

Snippet Source:

https://snipsnip.pro/s/825

History

Last modified:

29/01/2024

Important Note

This snippet has the following status:

AI Verified

This snippet has been tested by our AI bot, see any comments below.

AI Bot Comments:

Found 0 vulnerabilities

Disable Specific Plugins on Production Environments [SnipSnip.pro]

 
                    
1/**
2 * Disable Specific Plugins on Production Environments [SnipSnip.pro] - https://snipsnip.pro/s/825
3 */
4if (!class_exists('Selective_Plugin_Deactivator')) {
5 /**
6 * Selectively deactivates plugins based on the current environment.
7 */
8 class Selective_Plugin_Deactivator {
9 private $envKeywords = ['staging', 'dev', 'local', 'test', 'sandbox', 'demo'];
10 private $alwaysRunDeactivations = false; // Set to true to bypass environment check.
11 const PLUGINS_TO_DEACTIVATE = [
12 'query-monitor',
13 'create-block-theme',
14 //'ai-engine*'
15 ];
16 
17 public function __construct() {
18 // Skip environment check if alwaysRunDeactivations is true, or if not in a production environment.
19 if ($this->alwaysRunDeactivations || !$this->isCurrentEnvironmentProduction()) {
20 add_filter('option_active_plugins', [$this, 'deactivateSelectedPlugins']);
21 }
22 }
23 
24 /**
25 * Determines if the current environment is considered production.
26 *
27 * @return bool True if production, false otherwise.
28 */
29 private function isCurrentEnvironmentProduction() {
30 $currentUrl = strtolower(get_option('siteurl'));
31 
32 foreach ($this->envKeywords as $keyword) {
33 if (strpos($currentUrl, $keyword) !== false) {
34 return false;
35 }
36 }
37 
38 return true;
39 }
40 
41 /**
42 * Deactivates selected plugins from the active plugins list.
43 *
44 * @param array $activePlugins Currently active plugin paths.
45 * @return array Filtered plugin paths with selected plugins deactivated.
46 */
47 public function deactivateSelectedPlugins($activePlugins) {
48 foreach (self::PLUGINS_TO_DEACTIVATE as $pluginPattern) {
49 $pattern = strtolower($pluginPattern);
50 if (strpos($pattern, '*') !== false) {
51 $pattern = str_replace('*', '.*', $pattern);
52 $activePlugins = array_filter($activePlugins, function($plugin) use ($pattern) {
53 $pluginSlug = strtolower(substr($plugin, 0, strpos($plugin, '/')));
54 return !preg_match('/^' . $pattern . '$/', $pluginSlug);
55 });
56 } else {
57 $pluginPath = "$pattern/$pattern.php"; // Assumes standard plugin directory structure.
58 $key = array_search($pluginPath, $activePlugins);
59 if ($key !== false) {
60 unset($activePlugins[$key]);
61 }
62 }
63 }
64 
65 return array_values($activePlugins); // Ensure the array is reindexed
66 }
67 }
68 
69 new Selective_Plugin_Deactivator();
70}

0

Related Snippets

Please see some snippets below related to this snippet..

Performance

Unverified

1

Convert Uploaded Images to WebP Format

Added: 8 months ago

Last Updated: 6 months ago

Convert Uploaded Images to WebP Format

Performance

AI Verified

1

Ensure Webfont is Loaded (Non-Elementor)

Added: 1 year ago

Last Updated: 5 months ago

Ensure Webfont is loaded

Performance

AI Verified

0

Deactivate Specific Plugins on Staging & Non-Production Sites [SnipSnip.pro]

Added: 7 months ago

Last Updated: 7 months ago

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 n...

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

WordPress Admin

Unverified

1

Stop the Scroll! Float the Admin Notice Above the Editor in Code Snippets Pro [SnipSnip.pro]

Added: 10 months ago

Last Updated: 1 week ago

<p>Make the admin notice in Code Snippet Pro float above the editor so your page doesn't scroll away every time you save a snippet. <a href="https://snipsnip.pro/s/787" target="_blank" rel="nofollow n...

Security

Unverified

1

Users: Create & Delete n New Users in WP Admin [SnipSnip.pro] - SET TO RUN ONCE!

Added: 10 months ago

Last Updated: 1 week ago

<p>CHANGE TO 'ONLY RUN ONCE' – Users: Create &amp; Delete n New Users in WP Admin [SnipSnip.pro] - <a href="https://snipsnip.pro/s/799" target="_blank" rel="nofollow noopener">https://snipsnip.pro/s/7...

WordPress Admin

AI Verified

1

Randomize a Post Publish Date in the Classic Editor [SnipSnip.pro]

Added: 10 months ago

Last Updated: 10 months ago

<p>Randomize a Post Publish Date in the Classic Editor. Choose between the past 1, 3, 6, 12 months... or customize it to other options. Works in the Classic Editor only for now. <a href="https://snips...