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.
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.
-
$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 - You are checking for occurrence of a
test
string which unfortunately will also occur if someone has a PRODUCTION domain liketestyourIQ.com
or similar.
Please also note how you overcomplicate your IF statement with double negation (! stropos() === false
which is actually same asstropos() === 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.
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.
Fun fact #1 - The link above you have mentioned was written by me 5 years ago
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.
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 @ben I think some of it will land in the roots guides section here (I hope ;)).
Hey, thank you for the blog post!
I made a composer package out of it so it can reused it on all sites:
It also these plugins to the end of the WP Admin plugin list and adds a notice that it cannot be activated:
This is my first public plugin, so if anyone has ideas what can be improved please open an issue
Good job and great idea with the plugins page notification - that might be a good or even better alternative to logging that to error_log.
Ideas?
First of all I would ensure that in standard WP installation no one pushes this wp-config to prod. Your plugin suggests that’s its for non-production environments but that would only hold true for bedrock installations. Nothing prohibits the user from pushing that to prod and your instructions/naming may be misleading for less experienced users. Maybe you can make some instructions regarding how to protect your config from being run elsewhere.
Additionally if you really got inspired by my blogpost then I would appreciate some small credits that you were basing your solution on it. Obviously that’s your choice obviously.
Sure thing, I’ve added the credits. Thanks for the feedback! I’ve also added some instructions on how to add a a local config for non-Bedrock sites. Does that make sense to you?
EDIT:
It only works as a Must Use Plugin and therefore only for Bedrock sites (because of the MU Plugin Autoloader). The user gets a notice if he still tries to install and activate it as a regular plugin.
The plugin is shaping up very nicely. I am a bit jealous now
One note- even though you have it as mu-plugin then there are many bedrock alternatives out there which support mu-autoloading (wordplate is one of the examples or my library). Obviously you cannot and should not be ready for every possible use case
Good work!
Super helpful plugin, exactly what I was looking for. Thanks owi and luke!