Skip to content

Instantly share code, notes, and snippets.

@ezekg
Last active August 29, 2015 14:10
Show Gist options
  • Save ezekg/98a8bd7f7078d3127364 to your computer and use it in GitHub Desktop.
Save ezekg/98a8bd7f7078d3127364 to your computer and use it in GitHub Desktop.
Multiple WP environments with Dotenv and Composer
DB_NAME=database
DB_USER=user
DB_PASSWORD=pass
DB_HOST=localhost
WP_HOME=http://example.dev
WP_SITEURL=http://example.dev/wp
<?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