Skip to content

Instantly share code, notes, and snippets.

@khromov khromov/wp-config.php
Last active Oct 1, 2019

Embed
What would you like to do?
Convert MySQL connection string to wp-config.php parameters
<?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
You can’t perform that action at this time.