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
over 1 year ago
·

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
over 1 year ago
·

Cheers for the props :)
over 1 year ago
·

Ah, I see props are working again ;) You're welcome @mikeshawdev
over 1 year ago
·