Using the same wp-config.php file on local, dev, and on live server

If you don’t want to rewrite your wp-config.php every time you shift between your local dev environment and another server this trick will help you.

Set up the server configs as variables. This lets you connect to each individual database.

Replace the default lines with define(‘DB_NAME’, ‘dev_mywebsite’), DB_USER, DB_PASSWORD, and DB_HOST with this snippet. Don’t forget to change the variables (mywebsite, username, password, mysql) into your own settings, otherwise this won’t do much good for you.

switch($_SERVER[‘SERVER_NAME’]){
/** Local dev */
case ‘mywebsite.dev’:
$db_name = ‘dev_mywebsite’;
$db_user = ‘username’;
$db_pass = ‘password’;
$db_host = ‘localhost’;
break;
/** Staging */
case ’staging.mywebsite.com’:
$db_name = ’staging_mywebsite’;
$db_user = ‘username’;
$db_pass = ‘password’;
$db_host = ‘mysql.mywebsite.com’;
break;
/** Production */
default:
$db_name = ‘prod_mywebsite’;
$db_user = ‘username’;
$db_pass = ‘password’;
$db_host = ‘mysql.mywebsite.com’;
}
define(‘DB_NAME’, $db_name);
define(‘DB_USER’, $db_user);
define(‘DB_PASSWORD’, $db_pass);
define(‘DB_HOST’, $db_host);

I'm a Norwegian sharing my time between the mountains and the web.

I'm a Norwegian sharing my time between the mountains and the web.