Discover in this tutorial how to enable or disable plugins depending on the WordPress environment (staging, production…) using code.
In a previous tutorial you learned how to define a type of environment in WordPress (production, development, staging…).
The good thing about this is that it opens the door to use different configurations depending on the environment.
You can enable or disable plugins. Very practical, don’t you think?
Here’s how to do it:
Snippet to enable or disable plugins depending on the environment in WordPress.
First of all, make sure you have the environment defined as I explained in this tutorial.
Once you’ve done that, add the following code at the end of functions.php or in your functionality plugin:
With this snippet you are using the
wp_get_environment_type() function to determine which is the working environment and indicate that when it is staging the WP Rocket plugin should be deactivated.
Instead, when it detects that it is in production it will activate it.
This is a pretty simple example that you can customize to your liking. There are plugins, such as cache plugins, that are better to have deactivated to see the changes you make, but I invite you to add as many as you consider necessary.
You can see that by combining the
wp_get_environment_type() function with
deactivate_plugins you can simplify your workflow when moving from one environment to another.
Now you know not only how you can define the environment type of your WordPress installation (production, development, staging…), but also how to activate or deactivate plugins depending on the environment.
Any questions? I read you in the comments.
And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉