wp_get_environment_type()
Retrieves the current environment type.
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', 'staging', and 'production'. If not set, the type defaults to 'production'.
1 time — 0.000001 sec (speed of light) | 50000 times — 0.00 sec (speed of light)
No Hooks.
Return
String
. The current environment type.
Usage
wp_get_environment_type();
Examples
#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 ); } }
#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; }
#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. |