wp_get_environment_type()WP 5.5.0

Gets the current environment type: local, development, staging, production (default).

This function standardizes the application of different code logic for different development environments. For example, you can run one code on local, another during testing, and a third in production.

This function is hooked in at the earliest stage of loading and can be used before any hook and even in the SHORTINIT environment.

Setting the Environment Type

The environment type can be set in two ways: using the global system variable WP_ENVIRONMENT_TYPE or a constant with the same name.

putenv( 'WP_ENVIRONMENT_TYPE=development' );

// or
define( 'WP_ENVIRONMENT_TYPE', 'development' );

Possible values for the constant:

  • local — local. Since version 5.5.1
  • development — development.
  • staging — branch, stage, testing.
  • production — live site (default).

The constant value takes precedence over the system variable value.

If a value not in the list above is specified, the function will return the type production.

When setting the environment type to development, the debug mode is enabled - WP_DEBUG = true. If the WP_DEBUG constant is not set. See wp_initial_constants().

Also read the description of the function putenv().

In addition to the environment mode, in WordPress you can also set the development mode: wp_is_development_mode().

1 time — 0.000001 sec (speed of light) | 50000 times — 0.00 sec (speed of light)

No Hooks.

Returns

String. The current environment type.

Usage

wp_get_environment_type();

Examples

0

#1 Example from the WP Kernel - default setting of constant WP_DEBUG

if ( ! defined( 'WP_DEBUG' ) ) {
	if ( 'development' === wp_get_environment_type() ) {
		define( 'WP_DEBUG', true );
	} else {
		define( 'WP_DEBUG', false );
	}
}
0

#2 Another demonstration example

switch ( wp_get_environment_type() ) {
	case 'local':
	case 'development':
		do_nothing();
		break;

	case 'staging':
		do_staging_thing();
		break;

	case 'production':
	default:
		do_production_thing();
		break;
}
0

#3 Close the site from search engines, if it is a version of the site for development

/**
 * Close from search engines indexing for dev.
 *
 * @return void
 */
function kama_development_disable_indexing(){

	// do noting it's prod OR Admin
	if(
		in_array( wp_get_environment_type(), [ 'production', 'local' ], true )
		||
		current_user_can( 'administrator' )
	){
		return;
	}

	// HTTP header
	header( 'X-Robots-Tag: noindex' );

	// robots.txt
	add_filter( 'robots_txt', fn() => "User-agent: *\nDisallow: /", 999 );

	// <meta name='robots' content='noindex, follow' />
	add_filter( 'wp_robots', function( $robots ){
		$robots['noindex'] = true;
		$robots['nofollow'] = true;
		unset( $robots['follow'] );

		return $robots;
	}, 999 );

	// 403 for search agents
	$robots = 'libwww|Wget|LWP|damnBot|BBBike|spider|crawl|google|bing|yandex|msnbot';
	if( preg_match( "/$robots/i", $_SERVER['HTTP_USER_AGENT'] ) ) {
		http_response_code( 403 );
		die( 'Public Forbidden' );
	}

}

Now it just calls this function somewhere in the plugin or in the fucntions.php file:

kama_development_disable_indexing();

Changelog

Since 5.5.0 Introduced.
Since 5.5.1 Added the 'local' type.
Since 5.5.1 Removed the ability to alter the list of types.

wp_get_environment_type() code WP 7.0

function wp_get_environment_type() {
	static $current_env = '';

	if ( ! defined( 'WP_RUN_CORE_TESTS' ) && $current_env ) {
		return $current_env;
	}

	$wp_environments = array(
		'local',
		'development',
		'staging',
		'production',
	);

	// Add a note about the deprecated WP_ENVIRONMENT_TYPES constant.
	if ( defined( 'WP_ENVIRONMENT_TYPES' ) && function_exists( '_deprecated_argument' ) ) {
		if ( function_exists( '__' ) ) {
			/* translators: %s: WP_ENVIRONMENT_TYPES */
			$message = sprintf( __( 'The %s constant is no longer supported.' ), 'WP_ENVIRONMENT_TYPES' );
		} else {
			$message = sprintf( 'The %s constant is no longer supported.', 'WP_ENVIRONMENT_TYPES' );
		}

		_deprecated_argument(
			'define()',
			'5.5.1',
			$message
		);
	}

	// Check if the environment variable has been set, if `getenv` is available on the system.
	if ( function_exists( 'getenv' ) ) {
		$has_env = getenv( 'WP_ENVIRONMENT_TYPE' );
		if ( false !== $has_env ) {
			$current_env = $has_env;
		}
	}

	// Fetch the environment from a constant, this overrides the global system variable.
	if ( defined( 'WP_ENVIRONMENT_TYPE' ) && WP_ENVIRONMENT_TYPE ) {
		$current_env = WP_ENVIRONMENT_TYPE;
	}

	// Make sure the environment is an allowed one, and not accidentally set to an invalid value.
	if ( ! in_array( $current_env, $wp_environments, true ) ) {
		$current_env = 'production';
	}

	return $current_env;
}