Wordpress - Local or live database
Instead of changing wp-config, each time you start working locally or push it live.
if ("localhost" == $_SERVER['HTTP_HOST']) {
define('DB_NAME', 'local_databasename');
define('DB_USER', 'local_password');
define('DB_PASSWORD', 'local_password');
define('DB_HOST', 'local_host');
define('WP_DEBUG', true);
} else {
/** MySQL database name */
define('DB_NAME', 'live_databasename');
/** MySQL database username */
define('DB_USER', 'live_user');
/** MySQL database password */
define('DB_PASSWORD', 'live_password');
/** MySQL hostname */
define('DB_HOST', 'live_host');
define('WP_DEBUG', false);
//define('FS_METHOD', 'direct');
}
Props @mikeshawdev
Written by pimschaaf
Related protips
6 Responses
If you have multiple environments, you could also use gethostname() and a switch statement to swap between connection details as appropriate
You could also put other settings such as WP_DEBUG in your switch, so your dev environment will always have debugging turned on and your live one won't
Cheers for the props :)
Ah, I see props are working again ;) You're welcome @mikeshawdev
Another interesting way of storing environment dependent variables, is to set them up as actual environment variables; completely extracting the environment variables from wp-config.php.
Roots.io has comprehensive documentation that could be an improvement to the workflow proposed in this tip. See: http://roots.io/twelve-factor-03-config
Aye, that looks good that
Just to add another idea to the mix, Mark Jaquith (in his WordPress skeleton: https://github.com/markjaquith/WordPress-Skeleton) has a local-config file. In his wp-config file he does a file_exists check for it which will load in different settings