Skip to content

Environment variables

Keep keys, tokens, and passwords out of your repository with per environment variables, and read WPWATERFALL_ENV in code to tell live, staging, and local apart.

Environment variables hold the values your code needs but your repository should not: API keys, tokens, passwords, and settings that differ between live and staging. They are per environment, so staging can use a test payment account while live uses the real one. WP Waterfall also sets two variables for you that tell your code which kind of environment it runs in (see Tell live, staging, and local apart in code).

How do I add a variable?

  1. Open your site, then Info, and find the Environment variables card for the environment you are on.
  2. Choose Add variable.
  3. Enter the name: capital letters, digits, and underscores, starting with a letter, up to 128 characters. Names that start with WPWATERFALL_ belong to the platform and are refused.
  4. Enter the value, up to 65,536 characters.
  5. Tick Secret if the value should never show again, then choose Save variable.

WP Waterfall applies the change through a job. The value is in place on the next build or PHP restart. A value set here replaces a default with the same name in wpwaterfall.yml.

How do I read a variable in PHP?

Every variable is an environment variable on each request. Read it with getenv(), $_ENV, or $_SERVER:

define('MY_SERVICE_KEY', getenv('MY_SERVICE_KEY'));

WP-CLI and SSH scripts see the same names, so a cron script reads the value your pages do. Check that a variable exists before you use it, because one set on staging does not exist on live.

How do secrets work?

A secret is masked after you save it, in the list and in the API. To change one, set it again with a new value. Nobody can read the old one back.

How do I change or delete a variable?

Choose Edit or Delete on its row. Each change applies through a job and takes effect on the next build or restart. Deleting asks you to type the name.

What goes in wpwaterfall.yml instead?

Values that are safe to share, such as a feature flag or a public URL, can live in the env section of wpwaterfall.yml. They travel with the repository and apply to every environment that builds from it. Keep secrets here, not there.

Tell live, staging, and local apart in code

WPWATERFALL_ENV tells PHP whether code runs on live, staging, a preview, or your own computer, and WPWATERFALL_ENV_NAME gives the environment's name. WP Waterfall sets both on every environment. Use them for settings that differ between environments, such as a payment mode or a cache plugin that must stay off outside live.

Environment WPWATERFALL_ENV WPWATERFALL_ENV_NAME WP_ENVIRONMENT_TYPE
Live live live production
Staging staging staging staging
A preview environment preview the preview's name, in lowercase letters, digits, and dashes development
Your computer (DDEV) local local local
  • WPWATERFALL_ENV is the type. Branch on this one.
  • WPWATERFALL_ENV_NAME is the name. For a preview called Feature/Login it is feature-login. A preview named live or staging reads preview-live or preview-staging, so no code mistakes it for the real thing.
  • WP_ENVIRONMENT_TYPE is the value WordPress core reads with wp_get_environment_type(). WP Waterfall sets it unless you already set it yourself, with a variable of that name.

How do I read them?

The values exist in four forms, so any plugin or script can read them:

// A constant, set at the top of wp-config.php before anything else loads. Works in any plugin or theme.
if ( WPWATERFALL_ENV === 'live' ) { /* ... */ }

// A process environment variable. Works in PHP, WP-CLI, SSH sessions, and cron.
if ( getenv( 'WPWATERFALL_ENV' ) === 'live' ) { /* ... */ }

// The $_SERVER copy, set for every web request.
$name = $_SERVER['WPWATERFALL_ENV_NAME'] ?? '';

// A helper from the WP Waterfall plugin, for code that runs after plugins load.
if ( wpwaterfall_env() === 'live' ) { /* ... */ }

The plugin helpers are wpwaterfall_env() for the type, wpwaterfall_env_name() for the name, and wpwaterfall_is_env( 'staging', 'preview' ) to test for more than one type. They answer an empty string, never live, when the platform has not said.

$_ENV is filled for web requests too, but PHP leaves it empty in some setups. Use getenv() or the constant instead.

Example: settings that differ per environment

WP Waterfall writes wp-config.php for each environment and keeps it out of Git, so branch in code that is in your repository: a must-use plugin in wp-content/mu-plugins, a plugin, or your theme's functions.php. On your computer you can also branch in wp-config-local.php. This keeps payments in test mode everywhere but live:

if ( getenv( 'WPWATERFALL_ENV' ) === 'live' ) {
	define( 'MYSHOP_MODE', 'live' );
} else {
	define( 'MYSHOP_MODE', 'test' );
}

Or turn a feature off outside live from a plugin or a theme's functions.php:

add_filter( 'myplugin_send_real_emails', fn() => wpwaterfall_env() === 'live' );

Memory, run time, and upload limits are not set this way. They belong to each environment under Tools, PHP settings. See Change PHP settings.

What stays true when environments change?

WP Waterfall works the values out from the environment's name each time it writes wp-config.php. They are never stored in your database, your Git repository, or a release. This has three results:

  • The same release runs as staging and as live with different values.
  • Cloning, promoting, copying staging into live, restoring a backup, and importing a database never carry staging into live.
  • You cannot change them. A variable or constant you add with a name that starts with WPWATERFALL_ is ignored.

The DDEV add-on sets WPWATERFALL_ENV=local and WPWATERFALL_ENV_NAME=local for the web container and in the local wp-config.php, so the same code that branches on live runs unchanged on your computer.

Quick answers

My site still uses the old value. Deploy, or restart PHP from Tools.

Do variables copy to a new environment? No. Set them on each environment that needs them.

Where does wp-config.php get its settings? WP Waterfall writes wp-config.php for each environment. It is never in Git.

Can I use these in a cron job or a WP-CLI command? Yes. Cron jobs, SSH sessions, and WP-CLI jobs started from the dashboard all get both variables.

Why is WPWATERFALL_ENV preview and not the name of my preview? The type tells you what kind of environment this is. The name is in WPWATERFALL_ENV_NAME.

Can I set WPWATERFALL_ENV myself to test live behavior? No. It would defeat the point: code that checks for live could then run on staging. Use WP_ENVIRONMENT_TYPE or your own variable for tests.

API

  • GET /v1/environments/{id}/env-vars
  • PUT /v1/environments/{id}/env-vars
  • DELETE /v1/environments/{id}/env-vars

Still stuck?

Email support at wpwaterfall dot com with your site name and what you tried, or send us a message.