Azure App Service WordPress Database connection wp-config.php

For connect do Azure App Service MySQL in App you need to modify wp-config.php file on the top:


$connectstr_dbhost = '';
$connectstr_dbname = '';
$connectstr_dbusername = '';
$connectstr_dbpassword = '';

foreach ($_SERVER as $key => $value) {
    if (strpos($key, "MYSQLCONNSTR_localdb") !== 0) {
        continue;
    }
    
    $connectstr_dbhost = preg_replace("/^.*Data Source=(.+?);.*$/", "\\1", $value);
    $connectstr_dbname = preg_replace("/^.*Database=(.+?);.*$/", "\\1", $value);
    $connectstr_dbusername = preg_replace("/^.*User Id=(.+?);.*$/", "\\1", $value);
    $connectstr_dbpassword = preg_replace("/^.*Password=(.+?)$/", "\\1", $value);
}


define('DB_NAME', $connectstr_dbname);

define('DB_USER', $connectstr_dbusername);

define('DB_PASSWORD', $connectstr_dbpassword);

define('DB_HOST', $connectstr_dbhost);