Skip to content

Instantly share code, notes, and snippets.

@macariojames
Last active September 1, 2019 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macariojames/acf65d9b71fc68b924dd84bb968266d3 to your computer and use it in GitHub Desktop.
Save macariojames/acf65d9b71fc68b924dd84bb968266d3 to your computer and use it in GitHub Desktop.
WordPress: Use different MySQL login credentials depending on your host (localhost, sandbox, production, etc.)
<?php
/* I use this in a separate config-properties.php or otherwise named file
* which is then included by wp-config.php ~mj */
function getRealHost() {
list($realHost,) = explode(':',$_SERVER['HTTP_HOST']);
return $realHost;
}
$realHost = getRealHost();
$local_db = true;
if ( ($realHost === 'localhost') && ($local_db) ) {
$db_name = 'local_db';
$db_user = 'local_user';
$db_pwd = 'password';
$db_host = 'ip_address';
} else if ( $realHost === 'sandbox.server' || $realHost === 'www.sandbox.server' ) {
$db_name = 'sandbox_db';
$db_user = 'sandbox_user';
$db_pwd = 'password';
$db_host = 'ip_address';
} else if ( $realHost === 'production.server' || $realHost === 'www.production.server' ){
$db_name = 'production_db';
$db_user = 'production_user';
$db_pwd = 'password';
$db_host = 'ip_address';
} else {
$db_name = 'db_name';
$db_user = 'root';
$db_pwd = 'root';
$db_host = 'localhost';
}
define('DB_NAME', $db_name); //The name of your database. Note that this database must exist before running installer.php
define('DB_USER', $db_user); //Your database username
define('DB_PASSWORD', $db_pwd); //Your database users password
define('DB_HOST', $db_host); //The hostname for your database
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment