Skip to content

Instantly share code, notes, and snippets.

@Brendonwbrown
Last active December 16, 2015 18:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Brendonwbrown/5478023 to your computer and use it in GitHub Desktop.
Save Brendonwbrown/5478023 to your computer and use it in GitHub Desktop.
Update your WP-Config DB settings based on current environment.
# WP-Config File Changer
## Credit to Konstantin Kovshenin, a complete stranger who publishes nice things on the web.
### Nutshell: customize this code with various DB information and place it in wp-config.php file instead of corresponding config information. It instructs Wordpress to look for a different (empty) file in parent directory of local, development or production servers and serves up the appropriate config to wordpress.
### You will have to create empty env_local, env_development files in parent directories of your websites on corresponding servers.
<?php
if ( file_exists( dirname( __FILE__ ) . '/../env_local' ) ) {
// Local Environment
define('WP_ENV', 'local');
define('WP_DEBUG', true);
define('DB_NAME', 'local_db_name');
define('DB_USER', 'local_db_user');
define('DB_PASSWORD', 'local_db_password');
define('DB_HOST', 'local_db_host');
} elseif ( file_exists( dirname( __FILE__ ) . '/../env_development' ) ) {
// Development Environment
define('WP_ENV', 'development');
define('WP_DEBUG', true);
// ... development db constants
} else {
// Production Environment
define('WP_ENV', 'production');
define('WP_DEBUG', false);
// ... production db constants
}
?>
# Let's say that my site installation is in a directory called "website" so the config file resides in website/wp-config.php.
# The code first looks for a file called env_local in a directory one level up from wp-config.php (which is "Sites" in my case) and if it’s there, defines the database settings for the local environment. If it doesn’t exist it looks for one called env_development (testing/staging server) and defines the database settings for my testing server. If none of the two exist it defaults to the production database parameters.
# I recommend putting env_local, env_playground and env_production into .gitignore.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment