Last active
August 29, 2015 14:10
-
-
Save ezekg/98a8bd7f7078d3127364 to your computer and use it in GitHub Desktop.
Multiple WP environments with Dotenv and Composer
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
DB_NAME=database | |
DB_USER=user | |
DB_PASSWORD=pass | |
DB_HOST=localhost | |
WP_HOME=http://example.dev | |
WP_SITEURL=http://example.dev/wp |
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
<?php | |
// Require Composer autoloader, which includes DotEnv | |
require_once __DIR__ . "/vendor/autoload.php"; | |
/** | |
* Check if environment exists in order of $stages array | |
* | |
* @return {String} $environment | |
*/ | |
$environment = function() { | |
$stages = array( | |
"development", | |
"staging", | |
"production" | |
); | |
foreach ( $stages as $stage ) { | |
if ( file_exists( __DIR__ . "/.env.$stage" ) ) { | |
return $stage; | |
} | |
} | |
return false; | |
}; | |
/** | |
* Use Dotenv to set stage | |
*/ | |
if ( $stage = $environment() ) { | |
Dotenv::load( __DIR__, ".env.$stage" ); | |
} else { | |
throw new \Exception( "Cannot load environment. Aborting mission." ); | |
} | |
Dotenv::required(array( | |
"DB_NAME", | |
"DB_USER", | |
"DB_PASSWORD", | |
"DB_HOST", | |
"WP_HOME", | |
"WP_SITEURL" | |
)); | |
/** | |
* Activate debugging on development stage | |
*/ | |
if ( $stage === "development" ) { | |
define( "WP_DEBUG", true ); | |
define( "WP_CACHE", false ); | |
} else { | |
define( "WP_DEBUG", false ); | |
define( "WP_CACHE", true ); | |
} | |
/** | |
* Database | |
*/ | |
define( "DB_NAME", getenv("DB_NAME") ); | |
define( "DB_USER", getenv("DB_USER") ); | |
define( "DB_PASSWORD", getenv("DB_PASSWORD") ); | |
define( "DB_HOST", getenv("DB_HOST") ); | |
/** | |
* URLs | |
*/ | |
define( "WP_HOME", getenv("WP_HOME") ); | |
define( "WP_SITEURL", getenv("WP_SITEURL") ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment