Skip to content

Instantly share code, notes, and snippets.

@khromov
Last active October 1, 2019 08:58
Show Gist options
  • Save khromov/53bbe3eeb0112ff249a865c0dd165737 to your computer and use it in GitHub Desktop.
Save khromov/53bbe3eeb0112ff249a865c0dd165737 to your computer and use it in GitHub Desktop.
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