Skip to content

Manage environment variables

Environment variables are a method for injecting information into WordPress and Node.js environments.

Environment variables are commonly used to:

  • Provide sensitive values (such as API secrets) to an application without committing the values to a site’s GitHub repository or storing the values in a site’s database.
  • Provide flags that can be used to vary behavior between environments. For example, an environment variable can be set to provide one value to a production environment and a different value to a develop environment.

Limitations

  • Each VIP Platform environment has its own discrete set of environment variables. For an environment variable to be available on all environments, it must be set on each environment separately.
  • An environment variable’s key is limited to 255 bytes.
  • An environment variable’s value is limited to 4096 bytes.
  • The overall limit of all keys and values of an environment’s environment variables is 16384 bytes.

Reserved names

Some environment variable names are reserved:

  • The prefixes WPCOM_ and VIP_ are reserved and cannot be used for environment variable naming.
  • To avoid conflicts, names reserved for WordPress constants (e.g., DB_NAME) cannot be used as environment variables.

Prerequisites

Manage environment variables with VIP-CLI

Manage environment variables with the subcommands of the VIP-CLI command: vip config envvar [options] [command]

Add or update a value

To add or update an environment variable and assign it a value: vip config envvar set [options]

--from-file

Accepted values: A relative or absolute path to a UTF-8-encoded text file

The --from-file option extracts a string from the file existing at the relative or absolute path in the passed value.

Use of the --from-file option is required for a string that has multiple lines created by newlines.

For example, to assign the multiline contents of a *.pem file as the value for the environment variable EXAMPLE_ENV_VAR:

vip @mytestsite.develop config envvar set EXAMPLE_ENV_VAR --from-file=/path/to/public.pem

Accessing environment variables in an application

Environment variables are always provided as strings, even if their value resembles another data type (e.g., "true""123""[1, 2, 3]"). String values can be converted or deserialized by an application into the desired data type, but they should be validated thoroughly.

Caution

New or updated environment variables will not be available until the application’s next code deployment.

On the VIP Platform, environment variables are accessed differently depending on the application type.

Node.js

Environment variables are provided as actual environment variables and are accessed using process.env. For example:

const apiKey = process.env.EXAMPLE_ENV_VAR || null;

WordPress

Environment variables are accessed using the Environment::get_var() helper method or vip_get_env_var helper function. For example:

<?php
// Using class method
$api_key = Automattic\VIP\Environment::get_var( 'EXAMPLE_ENV_VAR', $default_value );
// Using helper function
$api_key = vip_get_env_var( 'EXAMPLE_ENV_VAR', $default_value );

Note

Both the the Environment::get_var() helper method and the vip_get_env_var helper function are available for use in vip-config.php.

Managing environment variables for a VIP Local Development Environment

VIP-CLI can only be used to manage environment variables on VIP Platform environments. On a VIP Local Development Environment, an environment variable must be manually added as a defined constant.

This method will only work as expected if:

  • The --app-code option is set to a path to a git clone of an application’s wpcomvip GitHub repository on the local machine.
  • The constant for the added environment variable includes the prefix: VIP_ENV_VAR_.

Definitions for constants can be added to any file loaded by WordPress, but it is recommended to create a file in the client-mu-plugins directory that is dedicated to defining constants for the local environment. After creating the file, add it to .gitignore (e.g., client-mu-plugins/*.local.php) to avoid committing sensitive values to source control.

In this example, a file named client-mu-plugins/vip-env-vars.local.php has been created and a definition for the environment variable MY_CUSTOM_VAR is added:

client-mu-plugins/vip-env-vars.local.php
<?php
/**
 * Local environment variables
 */

define( 'VIP_ENV_VAR_MY_CUSTOM_VAR', 'my custom value' );

Once defined, the same methods for accessing environment variables in a VIP Platform environment can be used on the local environment:

$my_value = vip_get_env_var( 'MY_CUSTOM_VAR', $default_value );

Last updated: December 22, 2022