Let’s say, you’re working on a WordPress project where you have your local machine, staging environment (environment setup which is the same as or close to production), and production environment (deployed to the world wide web).
WordPress has this config file: wp-config.php. The problem with this is you have to separately configure it between different environments. For example moving from local setup to staging setup, you can sync both folders directly and set the staging config file as read-only. But the problem with this is you have to separately update both the staging and production config files.
Looking around, I stumbled upon WordPress-Config-Bootstrap on GitHub. It uses an array map of the environment and its domain, simple but smart:
$environments = array(
'local' => '.local',
'development' => '.dev',
'staging' => 'stage.',
'preview' => 'preview.',
);
See the rest of the code here.
I forked it and came up with my own version where there are separate files for each environment. I think that it’s easier to maintain this way than compressing all the config variables for different environments in one file together.
The wp-config.php file will look like this:
// Update this according to your needs
$environment_map = array(
'blog.yoursite.local' => 'local',
'blog.yoursite.staging' => 'staging',
'blog.yoursite.com' => 'production'
);
// Get Server name
$server_name = $_SERVER['SERVER_NAME'];
define('ENVIRONMENT', $environment_map[$server_name]);
define('ABSPATH', dirname(__FILE__) . '/');
$env_config = ABSPATH . sprintf('wp-config-%s.php', ENVIRONMENT);
// Terminate if file doesn't exist
// if(!file_exists(($env_config))) exit("$env_config doesn't exist");
/** Sets up WP config based on environment. */
require_once($env_config);
This is the part that determines which config file to use:
You will need to create one config file for each environment e.g. wp-config-local.php, wp-config-production.php, etc.












