Bedrock PHP file using .env custom variables

Hi we are using Bedrock (without Trellis) and currently testing a setup of an auto deploy script for theme updates from Github - which works well.

Problem is that the php file sits in the /web directory and has API keys etc. displaying so not good in terms of security.

I have tried adding custom vars to the .env for the API but the autodeploy no longer works when testing

So currently with the info coded into the file is works: e.g.

const API_KEY = "xxxxxxxxxx";
const API_URL = "https://xxxxxxxxxx";
const EMAIL = "xxxxxxxxxx@xxxxxxxxxx.com";
const BranchName = "xxxxxxxxxx";
const GitUrl = "git@github.com:xxxxxxxxxx";

So I tried replacing that with:

use function Env\env;

$test_api = $_ENV['TEST_API_KEY'];
$test_api_url = $_ENV['TEST_API_URL'];
$test_email = $_ENV['TEST_EMAIL'];
$test_branch = $_ENV['TEST_BRANCH'];
$test_git_url = $_ENV['TEST_GIT_URL'];

const TEST_API_KEY = "$test_api";
const TEST_API_URL = "$test_api_url";
const TEST_EMAIL = "$test_email";
const TEST_BranchName = "$test_branch";
const TEST_GitUrl = "$test_git_url";

also tried to match syntax in config/application.php:

use function Env\env;

$test_api = env('TEST_API_KEY');
$test_api_url = env('TEST_API_URL');
$test_email = env('TEST_EMAIL');
$test_branch = env('TEST_BRANCH');
$test_git_url = env('TEST_GIT_URL');

const TEST_API_KEY = "$test_api";
const TEST_API_URL = "$test_api_url";
const TEST_EMAIL = "$test_email";
const TEST_BranchName = "$test_branch";
const TEST_GitUrl = "$test_git_url";

None of these work - just wondering where I am going wrong or if it is even possible to pull .env variables into a custom file?

Any help would be greatly appreciated - thanks.

In your .env file: TEST_API_KEY='xxx'
In your config/application.php: env('TEST_API_KEY'). You need to add this after Dotenv has loaded your .env file (after line 43).

Is the PHP file a standalone file that is called directly like https://site.void/file.php ? If so, WordPress is probably not loaded and neither is Env etc. and you will have to include them yourself.

I would start by dumping $test_api to see if it gets the value you expect and if not work from there.

I would also guess that for example const TEST_API_KEY = "$test_api"; should be const TEST_API_KEY = $test_api; without the quotes, but then again PHP works in mysterious ways :slight_smile:

Thanks for the replies @folbert and @tombro - yes the PHP file is standalone hence I was trying same as application.php after open php tag:

use Roots\WPConfig\Config;
use function Env\env;

But that seemed to break the file loading so presume I haven’t called something correctly - @folbert yes you are right about quotes I was playing around with a string then trying the var but left the quote in where I copied/pasted.

So I added the vars to the .env and config/application.php after dotenv so just wondering what the correct syntax is to call the variable then in the standalone php file?

Should it just be as before? As it still doest seem to work.

$_ENV['TEST_API_KEY'];

Sorry I’m no expert in PHP but have a good enough understanding of it just this is a bit of a learning curve in terms of adding custom env variables.

Bedrock is built load all PHP through WordPress, so a standalone PHP file will never be executed in a Bedrock context.

If your file is in site/web then you may be able to just require_once( 'wp-config.php' ) at the top of it and have access to all of Bedrock’s variables, dependencies, etc.

2 Likes

This topic was automatically closed after 42 days. New replies are no longer allowed.