Skip to content

Instantly share code, notes, and snippets.

@jloescher
Last active August 29, 2015 14:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jloescher/ad9e937747cf76ab5751 to your computer and use it in GitHub Desktop.
Save jloescher/ad9e937747cf76ab5751 to your computer and use it in GitHub Desktop.
Multi Environments | WP-Config.php
<?php
/** Absolute path to the WordPress directory. **/
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');
/** Define Environments **/
$environments = array(
'localhost' => 'localhost',
'development' => '',
'staging' => '',
'production' => ''
);
/** Get Server name **/
$server_name = $_SERVER['SERVER_NAME'];
/** Detect Environment **/
foreach($environments AS $key => $host){
if ($server_name === $host) {
define('ENVIRONMENT', $key);
break;
}
}
/** Failsafe if no environment defined **/
if (!defined('ENVIRONMENT')) {
echo '<h2>The detected host has not been defined, please define in the "Define Environments" section of the wp-config.php file.</h2>';
echo '<br />';
echo '<h3>Detected Host: ' . $server_name . '</h3>';
echo '<hr />';
} else {
/** Load configuration based on the server's environment if environment is defined **/
require_once ABSPATH . 'wp-config/wp-config.' . strtolower(ENVIRONMENT) . '.php';
}
/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment