Created
September 17, 2012 17:54
-
-
Save joeyyax/3738765 to your computer and use it in GitHub Desktop.
Environment switch for wp-config when working with multiple environments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ** MySQL settings ** // | |
// define environments | |
$environments = array( | |
'local-joeyyax' => array( | |
'address' => array( 'domain.dev', 'www.domain.dev' ), | |
'db' => array( | |
'name' => '', | |
'user' => 'root', | |
'password' => 'root', | |
'host' => 'localhost', | |
'charset' => 'utf8', | |
'collate' => '' | |
), | |
'ssl' => false, | |
'dev' => true | |
), | |
'staging' => array( | |
'address' => array( 'staging.domain.com' ), | |
'db' => array( | |
'name' => '', | |
'user' => '', | |
'password' => '', | |
'host' => 'localhost', | |
'charset' => 'utf8', | |
'collate' => '' | |
), | |
'ssl' => false, | |
'dev' => false | |
), | |
'production' => array( | |
'address' => array( 'domain.com', 'www.domain.com' ), | |
'db' => array( | |
'name' => '', | |
'user' => '', | |
'password' => '', | |
'host' => '', | |
'charset' => 'utf8', | |
'collate' => '' | |
), | |
'ssl' => false, | |
'dev' => false | |
) | |
); | |
// allows wildcards in the array to be searched | |
function better_in_array( $needle, $haystack ) { | |
foreach ($haystack as $value) { | |
if ( fnmatch( $value, $needle ) === true ) { | |
return true; | |
} | |
} | |
return false; | |
} | |
// loop through environments to get proper db credentials | |
foreach( $environments as $key => $env ) { | |
if ( better_in_array( $_SERVER['SERVER_NAME'] , $env['address'] ) ) { | |
// if we find a matching environment, apply db credentials | |
define( 'DB_NAME', $env['db']['name'] ); | |
define( 'DB_USER', $env['db']['user'] ); | |
define( 'DB_PASSWORD', $env['db']['password'] ); | |
define( 'DB_HOST', $env['db']['host'] ); | |
define( 'DB_CHARSET', $env['db']['charset'] ); | |
define( 'DB_COLLATE', $env['db']['collate'] ); | |
if ( $env['ssl'] == true ) { | |
define( 'FORCE_SSL_ADMIN', true ); | |
} | |
if ( $env['dev'] == true ) { | |
define( 'WP_DEBUG', true ); // Turn debugging ON | |
define( 'WP_DEBUG_DISPLAY', false ); // Turn forced display OFF | |
define( 'WP_DEBUG_LOG', true ); // Turn logging to wp-content/debug.log ON | |
} | |
$environment = $key; | |
break; // break foreach if we find a match | |
} | |
} | |
// make sure all db constants are set. | |
if ( !defined( 'DB_NAME' ) || !defined( 'DB_USER' ) || !defined( 'DB_PASSWORD' ) || !defined( 'DB_HOST' ) || !defined( 'DB_CHARSET' ) || !defined( 'DB_COLLATE' ) ) { | |
echo 'Environment not configured.'; | |
exit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment