Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Wordpress Multi-Environment wp-config.php Setup
<?php
/**
* This code is intended to be added to your wp-config.php file just below the top comment block.
* It should replace the existing define('DB_*') statements. You can add or remove sections
* depending on the number of environments you intend to setup. You should change all values of
* DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST for each environment, making them all distinct
* for security purposes.
*/
// determine the current environment
define('APPLICATION_ENV', (getenv('ENVIRONMENT') ? getenv('ENVIRONMENT') : 'production'));
// set specific global database variables depending on your environment
if (APPLICATION_ENV != 'production') {
// turn on error reporting
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', 1);
// set your database credentials for each of your environments
if (APPLICATION_ENV == 'local') {
define('DB_NAME', 'myLocalDatabase');
define('DB_USER', 'myLocalUser');
define('DB_PASSWORD', 'myLocalPassword');
define('DB_HOST', 'localhost');
} else if (APPLICATION_ENV == 'dev') {
define('DB_NAME', 'myDevDatabase');
define('DB_USER', 'myDevUser');
define('DB_PASSWORD', 'myDevPassword');
define('DB_HOST', '192.168.1.100');
} else if (APPLICATION_ENV == 'staging') {
define('DB_NAME', 'myStagingDatabase');
define('DB_USER', 'myStagingUser');
define('DB_PASSWORD', 'myStagingPassword');
define('DB_HOST', 'localhost');
}
} else {
// turn off error reporting in production
error_reporting(0);
@ini_set(‘display_errors’, 0);
// set your production database values
define('DB_NAME', 'myProductionDatabase');
define('DB_USER', 'myProductionUser');
define('DB_PASSWORD', 'myProductionPassword');
define('DB_HOST', 'localhost');
}
// no need to touch these unless you have specific requirements to do so
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');
@juniovitorino
Copy link

juniovitorino commented Mar 27, 2012

Hey buddy, really cool your approach, but let me know how you're setting ENVIRONMENT constant variable, because in production it'll behave as in development in my understanding, no?

@cballou
Copy link
Author

cballou commented Mar 28, 2012

You can use getenv() or apache_getenv() to retrieve variables that have been set inside of either Apache or inside of .htaccess, meaning you set the server environment directly in one of those two places:

# .htaccess version
SetEnv ENVIRONMENT "production"

# apache version
<VirtualHost *:80>
    <Directory />
        SetEnv ENVIRONMENT local
    </Directory>
</VirtualHost>

@juniovitorino
Copy link

juniovitorino commented Mar 28, 2012

Cool man, thanks.

@alexandrejulien
Copy link

alexandrejulien commented Mar 24, 2013

Nice stuff, for my part I use a Phing build script to switch wp-config files. But there is a problem with Wordpress, many configurations datas are stored in Database likes url host.

@MextroNL
Copy link

MextroNL commented Feb 12, 2021

Hi, I have a ubuntu server with database and files running on it, I commit changes to remote, pull them on my server and want to use my ubuntu database on windows pc, but i can't because of wp_options siteurl and home... how to make this work i use the same database as my server but i can develop on localhost

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment