WordPress Development

Managing WordPress Across Local, Staging and Production with Environment Types

Managing WordPress Across Local, Staging and Production with Environment Types

A client once received a puzzled email from a customer asking why they’d been sent an order confirmation for something they’d bought six months earlier. It turned out a developer had copied the production database to a staging site to test a WooCommerce update. The staging site still had live email settings, and a scheduled task resent a batch of old order emails to real customers. The WP_ENVIRONMENT_TYPE constant is the simplest way to tell them apart.

Variations of this happen all the time. Staging sites charge real cards because live payment keys came with the database. They get indexed by Google and compete with the live site. They send newsletters, post to social media or sync with a CRM, all because they’re exact copies of production with nothing telling them they aren’t production.

WordPress has a simple, underused feature to help with this: environment types. Combined with a sensible configuration structure, it lets the same code behave correctly in each environment.

What WP_ENVIRONMENT_TYPE is

Since WordPress 5.5, you can declare which kind of environment a site is running in. There are four allowed values:

  • local for a developer’s own machine
  • development for shared development servers
  • staging for pre-production testing
  • production for the live site

You set it in wp-config.php:

Or as a server environment variable named WP_ENVIRONMENT_TYPE, which WordPress also reads.

If nothing is set, WordPress assumes production. That’s a sensible default, because it means forgetting to set it doesn’t accidentally disable anything important on the live site.

Code can then check the current environment:

What WordPress does with it

Core uses the environment type in a few places. For example, when it’s set to local, WordPress enables application passwords even without HTTPS, and development mode features become easier to use. Some hosts also set it automatically on their staging environments.

But the real power comes from what your own code and plugins do with it. Several well-known plugins already check it. Jetpack, for example, adjusts behaviour in staging environments, and some payment and email plugins recognise it too. Where they don’t, a small must-use plugin can fill the gaps.

The things that must be different outside production

Before writing any code, list what should never happen outside production on your site. Common items:

  • Sending emails to real customers or users
  • Processing live payments
  • Being indexed by search engines
  • Sending data to analytics, which pollutes reports
  • Syncing with CRMs, email marketing tools or inventory systems
  • Running scheduled tasks that contact external services
  • Posting to social media
  • Triggering webhooks to other systems

For each one, decide what should happen instead in staging and local environments.

A must-use plugin for non-production safety

Here’s an example that handles several of the common risks. Place it in wp-content/mu-plugins/environment-safety.php and deploy it to every environment. It only acts outside production.

That does three things. It forces “discourage search engines” on, regardless of what the database says. It redirects every email to a single developer inbox, adding the environment name and original recipients to the subject line. And it adds a label to the admin bar, so nobody forgets which site they’re editing.

The admin bar label sounds trivial, but I’ve lost count of the times someone made a change on production thinking it was staging, or the other way round. A bright label helps more than you’d think. Some people style it with a red background for staging.

Payment gateways

Payment gateways need special care because test and live keys are usually stored in the database. After copying production to staging, the live keys come with it.

Options:

  • Many gateways have a test mode setting. Force it on outside production through the gateway’s filters, if it provides them.
  • Store gateway keys in wp-config.php constants per environment, if the plugin supports that, so the database copy doesn’t matter.
  • As part of your database copy process, run a WP-CLI command that switches the gateway to test mode.

For WooCommerce with Stripe, for example, a post-copy script might update the gateway settings to test mode and set test keys. Make it part of the routine, not something you remember to do.

Keeping configuration in one place

The cleanest setups keep all environment-specific values outside the database and outside version control. A common pattern is a single wp-config.php committed to the repository that reads values from environment variables or a separate, uncommitted file.

Each server then has its own .env.php or environment variables. The same codebase deploys everywhere, and each environment gets the right configuration.

Projects built on Bedrock use a .env file and a config folder with separate files per environment, which is a well-tested version of the same idea.

Debug settings per environment

Debugging should be on where it helps and off where it would expose information:

On staging, I often enable WP_DEBUG_LOG but keep display off, so the site looks like production while still recording warnings.

Search engine protection, beyond the setting

The “discourage search engines” option only adds a noindex instruction. It doesn’t stop people or bots visiting. For staging, add HTTP basic authentication at the server level as well. It’s the most reliable way to keep staging out of search results and away from curious visitors.

If a staging site has already been indexed, add password protection, keep the noindex in place and request removal of the staging URLs through Google Search Console for that domain.

Scheduled tasks and integrations

WP-Cron events copied with the database will run on staging. Some of them may contact external services, such as syncing orders to an accounting tool or sending abandoned cart emails.

You can disable WP-Cron entirely on staging with DISABLE_WP_CRON and run only the tasks you’re testing manually. Or you can unschedule specific hooks in the safety plugin. For integrations with API keys, use separate sandbox credentials per environment, set in configuration rather than the database.

A routine for copying production to staging

Write the steps down, or better, script them:

  1. Copy files and database from production.
  2. Run a search-replace on the database to update the site URL.
  3. Confirm WP_ENVIRONMENT_TYPE is set to staging on the staging server.
  4. Switch payment gateways to test mode.
  5. Clear or anonymise sensitive customer data if the staging site is accessed by people who shouldn’t see it.
  6. Check that emails go to the developer inbox by triggering a test email.

Done consistently, this turns a risky manual process into a safe, repeatable one.

Local development tools and environment types

Most local WordPress tools let you set the environment type easily. In wp-env, the official Docker-based tool, the development environment is set to local automatically. Local by Flywheel, DDEV and Lando let you add constants or environment variables to the generated configuration. Make it part of your project template, so every new local site starts with the right value and nobody has to remember.

It’s also worth agreeing on conventions as a team. For example: local sites always use a .test domain, staging sites always use a staging subdomain protected by basic authentication, and production is the only environment where WP_ENVIRONMENT_TYPE is production. When the conventions are written down, mistakes become obvious, because something that looks like staging but reports production immediately stands out.

Checking the current environment quickly

When you’re unsure which environment you’re on, WP-CLI gives an instant answer:

Site Health also shows the environment type under the WordPress Constants section of the Info tab. Adding that check to your deployment scripts, and failing the deployment if a staging server reports production, is a simple safeguard against configuration files being copied to the wrong place.

Plugins that respect the environment

More plugins now read the environment type and adjust their behaviour automatically. Some backup plugins pause scheduled backups on non-production sites, some email logging plugins block sending outside production and some SEO plugins add noindex automatically on staging. Check the documentation of the plugins you rely on. Where a plugin offers this, you can remove the equivalent code from your own safety plugin and let the plugin handle it.

Multisite and environment types

On a multisite network, WP_ENVIRONMENT_TYPE applies to the whole network, because it’s defined once in wp-config.php. That’s usually what you want. Copy the production network to staging, set the constant once and every subsite behaves as staging. Set WP_ENVIRONMENT_TYPE on every environment you run, and make the safety plugin part of every new project.

Frequently asked questions

What happens if I don’t set WP_ENVIRONMENT_TYPE?

WordPress treats the site as production. Nothing breaks, but you lose the ability for code to recognise staging and local sites.

Do managed hosts set this automatically?

Some do on their staging environments. Check by calling wp_get_environment_type() or running wp eval “echo wp_get_environment_type();” on each environment.

Can I add my own environment types?

Only the four values are allowed. If you need finer distinctions, define your own additional constant alongside it.

Should staging use real customer data?

Ideally not. If you must copy production data for realistic testing, restrict access to staging, protect it with a password and consider anonymising personal data.