what is and how to use wp environment type constant and the wp get environment type function?

code snippet

In WordPress version 5.5 a new function wp_get_environment_type was added to differentiate actions depending on the state of a WordPress site: in production, local, testing or live.

It is a particularly useful feature for plugin and theme developers or hosting companies who want to launch different actions, depending on the development status of the site.

Values of the function wp_get_environment_type

Depending on the state of the web the different values that can be used are the following:

  • local – Local development environments.
  • development – Development environments.
  • staging – testing/staging environments.
  • production – When the website is already in production/live.

Uses of the function wp_get_environment_type

Possible uses that come to mind could be:

  • Hosting companies that, when creating a staging of your website for development of new features, deactivate the staging indexing and plugins that require automatic connection with external services. Then, when moving from staging to production, reactivate everything again.
  • SEO/Security/Optimization plugins that apply different configurations depending on the development status of the web. They are already taking their time on this.
  • WordPress installation applications in local environments that apply different configurations for this type of development status.

Examples of using wp_get_environment_type

A generic example of use would be something like this:

switch ( wp_get_environment_type() ) { case 'local': //Local environment case 'development': //Development environment do_nothing(); break; case 'staging': //Staging environment do_some_staging(); break; case 'production': //Live environment default: do_some_production(); break; }
Code language: JavaScript (javascript)

And, if we are looking at something specific, you could create a mu-plugin that modifies the robots.txt file if the site is in production, like this:

<?php /* Evade bots crawling in production site */ if ( 'production' !== wp_get_environment_type() ) { // Bots crawling. add_filter( 'robots_txt', 'wpdocs_name_block_crawling', 999 ); // Activate "Disable browsers index" add_filter( 'pre_option_blog_public', '__return_zero', 999 ); } /* We filter robots.txt to evade index environments that are not in production */ function wpdocs_name_block_crawling( $output ) { $output = '# Crawling block for not-in-production' . PHP_EOL; $output .= 'User-agent: *' . PHP_EOL; $output .= 'Disallow: /'; return $output; }
Code language: HTML, XML (xml)

How to set the type of environment

There are two ways in a WordPress installation to set the environment type, so that the plugin/theme/hosting can invoke actions depending on its state via the wp_get_environment_type function.

They are processed in the following order with each sequential option overriding any previous values:

  1. The PHP environment variable WP_ENVIRONMENT_TYPE.
  2. The WP_ENVIRONMENT_TYPE constant.

For both, if the environment value is not in the list of allowed environments, the default value will be production.

Development environments in wp-config.php

The easiest way is clearly by defining the constant in the wp-config.php file:

define( 'WP_ENVIRONMENT_TYPE', 'development' );
Code language: JavaScript (javascript)

As we saw before, the possible values are: local, development, staging or production, the latter being the default value, so if this is the case there is no need to add the constant.

As an additional note, when the WP_ENVIRONMENT_TYPE constant is defined as development, the DEBUG mode is automatically activated.


You’ll let me know but I find the wp_get_environment_type function really useful, especially for hosting companies and developers of conscientious plugins, don’t you think?

In any case, I hope that today you have learned something more about WordPress … that maybe you did not know.

Click to rate this post!
[Total: 0 Average: 0]

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top Skip to content