Convert MySQL connection string to wp-config.php parameters
This file contains 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 | |
//... | |
// This will usually be received from an env variable, for example $connectionString = getenv('DB_URL'); | |
$connectionString = 'mysql://user:password@foo-bar.rds.amazonaws.com:3306/database'; | |
// The values below will be your default values if $connectionString is empty. | |
$dbConfig = array_merge( | |
[ | |
'host' => 'localhost', | |
'port' => 3306, | |
'user' => 'root', | |
'pass' => '', | |
'path' => 'wp', // DB_NAME | |
], | |
array_filter(parse_url($connectionString)) | |
); | |
/** The name of the database for WordPress */ | |
define('DB_NAME', ltrim($dbConfig['path'], '/')); | |
/** MySQL database username */ | |
define('DB_USER', $dbConfig['user']); | |
/** MySQL database username */ | |
define('DB_PASSWORD', $dbConfig['pass']); | |
/** MySQL hostname */ | |
define('DB_HOST', "{$dbConfig['host']}:{$dbConfig['port']}"); | |
//... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment