Last active
January 19, 2016 08:34
-
-
Save KB1RMA/9dca61aa90582fad8d11 to your computer and use it in GitHub Desktop.
Here is a quick and dirty little trick I often use for faking environments in Wordpress installs for local development. The real trick here is just detecting whether or not a local config file exists and setting credentials appropriately.
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 | |
// Settings used for local development | |
// do NOT track in your repository for deployment | |
/** | |
* Following 2 lines overwrite the standard WP_HOME and WP_SITEURL in the | |
* wp_options table of the database. Doesn't solve all problems, but any | |
* URL's generated dynamically (You are using site_url('', 'relative'), aren't you?!) | |
* will generate correctly. | |
* | |
* Use http://wp-cli.org/commands/search-replace/ for anything else. | |
* | |
*/ | |
define( 'WP_HOME',"http://localsite.dev"); // no trailing slash | |
define( 'WP_SITEURL',"http://localsite.dev"); // no trailing slash | |
// Debug settings | |
define( 'WP_DEBUG', true ); | |
define( 'WP_DEBUG_LOG', true ); | |
// Local DB credentials | |
define( 'DB_NAME', '' ); | |
define( 'DB_USER', '' ); | |
define( 'DB_PASSWORD', '' ); | |
define( 'DB_HOST', 'localhost' ); |
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 | |
// Standard wp-config stuff here | |
/** | |
* First condition tests whether or not a local-config.php file exists | |
* which indicates we should be using the credentials specified in that | |
* file. Handy for local development without altering production credentials. | |
* | |
* WP_LOCAL_DEV is also defined which is handy if you need to add any debug code | |
* anywhere in your theme. | |
* | |
* If it doesn't exist, just carry on like you normally would in a wp-config.php file | |
*/ | |
if ( file_exists( dirname( __FILE__ ) . '/local-config.php' ) ) { | |
include( dirname( __FILE__ ) . '/local-config.php' ); | |
define( 'WP_LOCAL_DEV', true ); // We'll talk about this later | |
} else { | |
/** The name of the database for WordPress */ | |
define( 'DB_NAME', '' ); | |
define( 'DB_USER', '' ); | |
define( 'DB_PASSWORD', '' ); | |
define( 'DB_HOST', 'localhost' ); // Probably 'localhost' | |
define('WP_DEBUG', false); | |
} | |
// Standard wp-config stuff here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Credit where credit is due... this comes from: Mark Jaquith
The original article elaborates on (and uses) the WP_LOCAL_DEV var created above. ("We'll talk about this later")