• Resolved yariwp

    (@yariwp)


    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)
  • Hello there,

    Thanks for reaching out to us!

    At the moment I am not familiar with the Bedrock Environment so I can’t say for sure what is the issue on our end.

    In this case, install this plugin to switch to a WordPress native approach. The only drawback is that on some large websites it can be a bit slower to translate front-end dynamic texts. https://translatepress.com/download/1151690/

    Let me know about this.

    Thread Starter yariwp

    (@yariwp)

    Hey,

    Thanks for your response.

    I’ve tried the native WordPress approach, unfortunately this was a little bit too slow for my liking. So in order to still use trp-ajax I decided to create a composer patch with the code from my initial response, which seems to work fine as a temporary fix for now.

    Hopefully somewhere in the future support for Bedrock environments can be added, as this is slowly becoming more and more popular amongst WordPress developers (you can find more about it here: https://roots.io/bedrock/).

    Thank you.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘connect_to_db() fails with Bedrock environment’ is closed to new replies.