1

I have an environment variable that is set in wp-config.php, but it is not being picked by the plugin code. However, if I return variable itself in the plugin, it works fine. What am I missing?

// code in wp-config.php
define('MY_ENV_VARIABLE', 'some_string');

// code in my_plugin.php
function get_my_env_variable() {
    return getenv('MY_ENV_VARIABLE');
    //if I return "some_string" here instead, code works fine but not the getenv()
}
Les
  • 330
  • 5
  • 15

1 Answers1

4

The function define is used to create a global constant which is not the same as an environment variable. Environment variables are literally variables from the environment, including the OS as well as parts of the request or process.

Once you define something you can use it literally.

define('FOOD', 'pizza');
echo FOOD;

If you actually want to use environment variable, you can set them in your OS or server config, use putenv, or something like DotEnv.

putenv('FOOD=salad');
echo getenv('FOOD');

Demo: https://3v4l.org/8ivUV#v8.2.3

Chris Haas
  • 53,986
  • 12
  • 141
  • 274