connect_to_db() fails with Bedrock environment
-
Hi,
Whenever I try to use the functionality of trp-ajax.php on a Bedrock installation, the connect_to_db function will always return false presumably because the file_get_contents will attempt to load the raw contents of wp-config.php without the necessary require calls.
As a quick workaround I gathered the necessary database credentials directly from the .env file:protected function connect_to_db() { $root_dir = __DIR__ . '/../../../../../'; $dotenv = new Dotenv\Dotenv($root_dir); if (file_exists($root_dir . '/.env')) { $dotenv->load(); $dotenv->required(['DB_NAME', 'DB_USER', 'DB_PASSWORD', 'WP_HOME', 'WP_SITEURL']); } else { return false; } $credentials = array( 'db_name' => getenv('DB_NAME'), 'db_user' => getenv('DB_USER'), 'db_password' => getenv('DB_PASSWORD'), 'db_host' => getenv('DB_HOST'), ); $this->connection = mysqli_connect($credentials['db_host'], $credentials['db_user'], $credentials['db_password'], $credentials['db_name']); // Check connection if (mysqli_connect_errno()) { // Failed to connect to MySQL. return false; } mysqli_set_charset($this->connection, 'utf8mb4'); if ($dbPrefix = getenv('DB_PREFIX')) { $this->table_prefix = $dbPrefix; } else { $this->table_prefix = $this->sql_find_table_prefix(); if ($this->table_prefix === false) { return false; } } return true; }
This works, but it’s obviously not a clean nor ideal solution.
Is there any future support planned for environment files?
Thanks in advance.
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘connect_to_db() fails with Bedrock environment’ is closed to new replies.