Last active
November 6, 2018 19:49
-
-
Save dccampbell/bd88a07aa530c136c5c50c308fb1ec44 to your computer and use it in GitHub Desktop.
Wordpress config file w/ env support
This file contains hidden or 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 | |
/** | |
* Dynamic Wordpress Configuration File | |
* | |
* This config file is designed to be flexible for use in any environment without need for modification. | |
* It exposes most configurations to being set externally, such as via SetEnv in Apache/htaccess or PHP's putenv(). | |
* | |
* It will optionally load a /env.php file. If the file returns an array, the key/values will be loaded using putenv(). | |
* | |
* In most environments, the only essential settings are DB_NAME, DB_USER, and DB_PASSWORD. | |
*/ | |
/* Load Environment Vars */ | |
$env = envLoad(__DIR__.'/env.php'); | |
/* Environment Name */ | |
define('WP_ENV', envGet('WP_ENV', 'production')); | |
/* Database Settings */ | |
define('DB_HOST', envGet('DB_HOST', 'localhost')); | |
define('DB_NAME', envGet('DB_NAME', 'wordpress')); | |
define('DB_USER', envGet('DB_USER', 'root')); | |
define('DB_PASSWORD', envGet('DB_PASSWORD', '')); | |
define('DB_CHARSET', envGet('DB_CHARSET', 'utf8')); | |
define('DB_COLLATE', envGet('DB_COLLATE', '')); | |
$table_prefix = envGet('DB_PREFIX', 'wp_'); | |
/* Auth Keys and Salts */ | |
define('AUTH_KEY', envGet('AUTH_KEY', 'SomeRandomlyGeneratedSecureSalt1')); | |
define('SECURE_AUTH_KEY', envGet('SECURE_AUTH_KEY', 'SomeRandomlyGeneratedSecureSalt2')); | |
define('LOGGED_IN_KEY', envGet('LOGGED_IN_KEY', 'SomeRandomlyGeneratedSecureSalt3')); | |
define('NONCE_KEY', envGet('NONCE_KEY', 'SomeRandomlyGeneratedSecureSalt4')); | |
define('AUTH_SALT', envGet('AUTH_SALT', 'SomeRandomlyGeneratedSecureSalt5')); | |
define('SECURE_AUTH_SALT', envGet('SECURE_AUTH_SALT','SomeRandomlyGeneratedSecureSalt6')); | |
define('LOGGED_IN_SALT', envGet('LOGGED_IN_SALT', 'SomeRandomlyGeneratedSecureSalt7')); | |
define('NONCE_SALT', envGet('NONCE_SALT', 'SomeRandomlyGeneratedSecureSalt8')); | |
/* Debug Settings */ | |
define('WP_DEBUG', envGet('WP_DEBUG', false)); | |
define('SAVEQUERIES', envGet('SAVEQUERIES', false)); | |
define('SCRIPT_DEBUG', envGet('SCRIPT_DEBUG', WP_DEBUG)); | |
define('WP_DEBUG_LOG', envGet('WP_DEBUG_LOG', WP_DEBUG)); | |
define('WP_DEBUG_DISPLAY', envGet('WP_DEBUG_DISPLAY', false)); | |
/* Updating */ | |
define('AUTOMATIC_UPDATER_DISABLED', envGet('AUTOMATIC_UPDATER_DISABLED', false)); | |
define('DISALLOW_FILE_EDIT', envGet('DISALLOW_FILE_EDIT', false)); | |
define('DISALLOW_FILE_MODS', envGet('DISALLOW_FILE_MODS', false)); | |
define('WP_AUTO_UPDATE_CORE', envGet('WP_AUTO_UPDATE_CORE', 'minor')); | |
/* Other Settings */ | |
define('WP_CACHE', envGet('WP_CACHE', false)); | |
define('WP_MEMORY_LIMIT', envGet('WP_MEMORY_LIMIT', '128M')); | |
define('WP_MAX_MEMORY_LIMIT', envGet('WP_MAX_MEMORY_LIMIT', '512M')); | |
define('DISABLE_WP_CRON', envGet('DISABLE_WP_CRON', false)); | |
/* Define Remaining Configs */ | |
envDefine($env); | |
unset($env); | |
/* Env Functions */ | |
function envLoad($envFile) { | |
if(is_readable($envFile) && is_array($env = include $envFile)) { | |
foreach($env as $key=>$val) { | |
putenv("$key=$val"); | |
} | |
return $env; | |
} | |
return []; | |
} | |
function envDefine($configs) { | |
foreach($configs as $key=>$val) { | |
if(!is_numeric($key) && !defined($key)) { | |
define($key, $val); | |
} | |
} | |
} | |
function envGet($key, $default = null) { | |
$value = getenv($key); | |
if (!is_string($value)) { | |
return $default; | |
} | |
switch (strtolower($value)) { | |
case 'null': return null; | |
case 'true': return true; | |
case 'false': return false; | |
} | |
if (strpos($value, '"') === 0 && substr($value, -1) === '"') { | |
return substr($value, 1, -1); | |
} | |
return $value; | |
} | |
/* That's all, stop editing! Happy blogging. */ | |
/** Absolute path to the WordPress directory. */ | |
if ( !defined('ABSPATH') ) | |
define('ABSPATH', dirname(__FILE__) . '/'); | |
/** 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