Establishing consistency in the configuration settings across different environments (e.g., local, development, testing, production) is essential for ensuring a stable and reliable system. At the same time, the ability to cater to specific requirements of each environment is crucial to achieve optimal performance and functionality.

Example Use Case: Email Delivery on Non-Production Environments

Have you ever had an experience where your website sent emails to users unexpectedly? After spending a few hours debugging why this duplicate email or “TESTING” email went out, you realize that someone forgot to make a change to the email plugin routing when you pulled down the database from production?

While you should certainly add DB sanitization scripts to your automations, setting up configurations to ensure emails are rerouted on non-production environments can be very helpful. In a production environment, outgoing emails should go to real users while in non-production environments, emails shouldn’t be sent to real users.

An Approach for Environment Based Configuration in WordPress

WordPress has a concept of Must Use Plugins (a.k.a mu-plugins). Must-use plugins are plugins installed in a special directory inside the content folder which are automatically enabled on all sites in the installation. These plugins can be used to control configurations and settings within your WordPress website and can be leveraged to configure settings on a per-environment basis giving you the ability to quickly and easily adjust configurations for prod vs. non-prod environments. 

In an example of a mu-plugin below, the wp-reroute-email plugin will be activated and configured on non-production environments. In order to leverage this:

  1. Create a `environment-config.php` under `wp-content/mu-plugins` directory
  2. Use wp_get_environment_type()  to identify the environment.
  3. Enable/disable the plugin based on the environment type.
  4. add_filter can be utilized to change configurations based on the environment.

Please refer to the below piece of code:

<?php

require_once(ABSPATH . 'wp-admin/includes/plugin.php');

# List Development Plugins
$plugins = [
 'wp-reroute-email/wp-reroute-email.php'
];

if (wp_get_environment_type() !== "live") {
 // Enable wp-reroute-email plugin.
 foreach ($plugins as $plugin) {
   if(is_plugin_inactive($plugin)) {
    activate_plugin($plugin);
   }
 }
 // Update plugin configuration to set a rerouting email.
 add_filter( 'pre_option_wp_reroute_email_enable', '__return_true' );
 add_filter( 'pre_option_wp_reroute_email_address', '__return_reroute_email_address' );
}
else {
 // This will ensure wp-reroute-email is always disabled.
 foreach ($plugins as $plugin) {
   if(is_plugin_inactive($plugin)) {
    deactivate_plugins($plugin);
   }
 }
 add_filter( 'pre_option_wp_reroute_email_enable', '__return_false' );
}

/**
* Set a rerouting email address.
*/
function __return_reroute_email_address(): string {
 return 'test@example.com';
}

Reference:

Author
Blog Category
Components
Category
Title
More on WordPress
View Reference
Recent Blog
Digital Polygon Marketing Team attends Hubspot Inbound conference September 2024. Read our highlights from the event.
150
Image
hubspot sign at inbound 2024
Last week at Drupal GovCon, one of our very own, Jeremy Gatza, led an eye-opening workshop on WebOps techniques for technical project managers. Dive into our recap to discover WebOps strategies, scenarios, and skills for technical project managers.
149
Image
webops training
Our first Drupal event marked a dynamic return to community engagement with an evening packed full of networking, insightful presentations, and a touch of celebration.
148
Image
Drupal Logo