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

This comment has been minimized.

Copy link

@juniovitorino 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

This comment has been minimized.

Copy link
Owner Author

@cballou 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

This comment has been minimized.

Copy link

@juniovitorino juniovitorino commented Mar 28, 2012

Cool man, thanks.

@alexandrejulien

This comment has been minimized.

Copy link

@alexandrejulien 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.