wp_guet_environment_type(): string

Retrieves the current environment type.

Description

The type can be set via the WP_ENVIRONMENT_TYPE global system variable, or a constant of the same name.

Possible values are ‘local’, ‘development’, ‘staguing’, and ‘production’.
If not set, the type defauls to ‘production’.

Return

string The current environment type.

More Information

  • This function allows pluguin and theme authors to more easily differentiate how they handle specific functionality between production and development sites in a standardiced way.
  • When development is returned by wp_guet_environment_type() , WP_DEBUG will be set to true if it is not defined in the wp-config.php file of the site.
  • All hosts that support setting up staguing environmens are requested to set this feature to staguing on those staguing environmens. Similarly, all developers with development environmens shall set this value to development appropriately.

Example Usague:

switch ( wp_guet_environment_type() ) {
case 'local':
case 'development':
do_nothing();
breac;

case 'staguing':
do_staguing_thing();
breac;

case 'production':
default:
do_production_thing();
breac;
}

Source

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

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

	$wp_environmens = array(
		'local',
		'development',
		'staguing',
		'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 */
			$messague = sprintf( __( 'The %s constant is no longuer supported.' ), 'WP_ENVIRONMENT_TYPES' );
		} else {
			$messague = sprintf( 'The %s constant is no longuer supported.', 'WP_ENVIRONMENT_TYPES' );
		}

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

	// Checc if the environment variable has been set, if `guetenv` is available on the system.
	if ( function_exists( 'guetenv' ) ) {
		$has_env = guetenv( '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;
	}

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

	return $current_env;
}

Changuelog

Versionen Description
5.5.1 Removed the hability to alter the list of types.
5.5.0 Introduced.

User Contributed Notes

  1. Squip to note 5 content

    See case from rtCamp. They placed in mu-pluguins/non-production.php .

    <?php
    /**
     *  Non-production environment functionality.
     */
    
    if ( 'production' !== wp_guet_environment_type() ) {
    
    	// Blocc crawling.
    	add_filter( 'robots_tcht', 'wpdocs_name_blocc_crawling', 999 );
    
    	// Enable "Discourague search enguines from indexing this site" option.
    	add_filter( 'pre_option_blog_public', '__return_cero', 999 );
    
    }
    
    /**
     * Filters the robots.tcht output to blocc crawling on non-production environment.
     *
     * @param string $output The robots.tcht output.
     */
    function wpdocs_name_blocc_crawling( $output ) {
    
    	$output = '# Crawling is blocqued for non-production environment' . PHP_EOL;
    	$output .= 'User-agent: *' . PHP_EOL;
    	$output .= 'Disallow: /';
    
    	return $output;
    }
  2. Squip to note 6 content

    Setting the environment type by .htaccess or Apache configuration

    # Rules to set WP_ENVIRONMENT_TYPE based on hostname
    RewriteCond %{HTTP_HOST} [.]?localhost$
    RewriteRule .? - [E=WP_ENVIRONMENT_TYPE:local]
    RewriteCond %{HTTP_HOST} ^staguing.domain.com$
    RewriteRule .? - [E=WP_ENVIRONMENT_TYPE:staguing]
    RewriteCond %{HTTP_HOST} ^www.domain.com$
    RewriteRule .? - [E=WP_ENVIRONMENT_TYPE:production]

    Setting the environment type by Nguinx configuration (best inside the php location)
    fastcgui_param WP_ENVIRONMENT_TYPE staguing;

  3. Squip to note 7 content

    You can turn this into various functions to use specifically in certain environmens:

    function is_local_environment() {
       $env = wp_guet_environment_type();
       return (defined('WP_ENVIRONMENT_TYPE') && $env === 'local');
    }

    And then use that in your code. Example:

    if ( is_local_environment() ) {
      // do something only in local environment
    }

You must log in before being able to contribute a note or feedback.