Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save codingwithchris/8cd56a98dd19bc8d3225969762f179a2 to your computer and use it in GitHub Desktop.
Save codingwithchris/8cd56a98dd19bc8d3225969762f179a2 to your computer and use it in GitHub Desktop.
Set environment in wp-config.php file based on current URL
// ==============================================================================
// Set Current Environment
// ==============================================================================
/**
* Define Environment/URL pairs for this project.
*
* Please include any possible URLs in each
* environment as these will be checked against to set an environment constant
* which can be easily used to create custom environment functionality and
* configurations elsewhere in our application.
*
* Inspired by: http://selfteach.me/wordpress-multiple-environments/
* Inspired by: https://deliciousbrains.com/updating-woocommerce/
*
* @author Christopher Hahn
*/
$environments = array(
'local' => [
'localdevurl.local'
],
'staging' => [
'staging.stagingurl.com',
],
'production' => [
'productionurl.com'
]
);
// Get the current http host
$http_host = $_SERVER['HTTP_HOST'];
/**
* Loop over each of our environments, take a look at the environment array to
* see if a defined URL matches our HTTP_HOST, and then define our APP_ENVIRONMENT
* constant based on the results.
*
* @author Christopher Hahn
*/
foreach( $environments as $environment => $urls ) {
if( array_search( $http_host, $urls ) !== false ){
define( 'APP_ENVIRONMENT', $environment );
break;
}
}
/**
* If we get to this point and the environment is not yet defined, we are going to
* define our current environment as 'production' as a failsafe.
*
* @author Christopher Hahn
*/
defined( 'APP_ENVIRONMENT' ) or define( 'APP_ENVIRONMENT', 'production' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment