Created
May 5, 2019 16:11
-
-
Save codingwithchris/8cd56a98dd19bc8d3225969762f179a2 to your computer and use it in GitHub Desktop.
Set environment in wp-config.php file based on current URL
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
// ============================================================================== | |
// 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