Roots Discourse

How can I disable plugins based on WP_ENV?

Hi there! I’m using Bedrock, is there a way to disable certain plugins on a per-environment basis? For example, I use a caching plugin, and it’d be nice if I could automatically disable it on my dev environment.

Something like this? https://lowgravity.pl/blog/quick-tip-how-to-disable-wp-plugin-on-certain-page/

Nevermind, redacted.

1 Like

Hey @Simeon ,

Please do not take it personally but unfortunately even though your idea is good in principle it fails in execution and should not be copy-pasted by other developers.

Besides obvious code style issues (mixing PHP styles, using old array notation etc.) or using unnecessary/wrong WP functions like sanitize_text_field or wp_unslash there is some flaw in the logic.

  1. $http_host will return the host portion of the request so it will never contain /wp-admin portion you are checking for in your IF statement.
    So it will never disable that plugin in admin or AJAX requests
  2. You are checking for occurrence of a test string which unfortunately will also occur if someone has a PRODUCTION domain like testyourIQ.com or similar.
    Please also note how you overcomplicate your IF statement with double negation ( ! stropos() === false which is actually same as stropos() === true )

As I was solving similar issues recently in BEDROCK I would advise other method:
a) Define disabled plugins in your config for local environment like

define('DEV_DISABLED_PLUGINS', serialize([
	'autoptimize/autoptimize.php',
	'wp-super-cache/wp-cache.php'
]));

b) Create a mu-plugin which will check if constant have been defined and iterate through it disabling the plugins, For that purpose you can do something like (SUDO CODE AHEAD):

	if ( defined( 'DEV_DISABLED_PLUGINS' ) ) {
		$plugins_to_disable = unserialize( DEV_DISABLED_PLUGINS );
		if ( ! empty( $plugins_to_disable ) && is_array( $plugins_to_disable ) ) {

			//Do disabling here like in example from Simeon

			error_log( 'Locally disabled plugins: ' . var_export( $plugins_to_disable, true ) );
		}
	}

(serialize usage - yes I know PHP 7 allows for arrays being stored in constants but many people are probably still on 5.x)

This way you avoid all the issues connected with checking the domain or request by simply configuring that in BEDROCK configs.
ThIs way you can also disable plugins based on ANY env, for example staging one or even prod.

8 Likes

Ha, no problem if it’s that bad I’ll pull it. Was based largely on the link in the second post anyway. Thanks for the comprehensive feedback.

@Simeon

Fun fact #1 - The link above you have mentioned was written by me 5 years ago :slight_smile:

And again - please do not consider it as “that bad”. The idea was proper. No one writes perfect code and I would never try to criticise your effort if it would not have some potential harmful pieces inside.

2 Likes

If anyone is interested - I have written a short tutorial on how i think you should disable plugins based on the environment you are currently on.

As per discussion with @benword I think some of it will land in the roots guides section here (I hope ;)).

5 Likes