{"id":263,"date":"2020-09-25T19:34:41","date_gmt":"2020-09-25T19:34:41","guid":{"rendered":"https:\/\/localwp.com\/?post_type=help&#038;p=263"},"modified":"2023-08-04T19:14:35","modified_gmt":"2023-08-04T19:14:35","slug":"using-wp_get_environment_type-to-code-for-different-environments","status":"publish","type":"help","link":"https:\/\/localwp.com\/help-docs\/advanced\/using-wp_get_environment_type-to-code-for-different-environments\/","title":{"rendered":"Use wp_get_environment_type() to Code for Different Environments"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Have you ever found yourself in a situation where you needed to have unicorn riding space-cats appear only in your Local environment?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yeah, me too.<\/p>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image size-large is-style-drop-shadow\"><img decoding=\"async\" width=\"1024\" height=\"768\" src=\"https:\/\/localwp.com\/wp-content\/uploads\/2020\/09\/image-1024x768.png\" alt=\"\" class=\"wp-image-264\" srcset=\"https:\/\/localwp.com\/wp-content\/uploads\/2020\/09\/image-1024x768.png 1024w, https:\/\/localwp.com\/wp-content\/uploads\/2020\/09\/image-300x225.png 300w, https:\/\/localwp.com\/wp-content\/uploads\/2020\/09\/image-768x576.png 768w, https:\/\/localwp.com\/wp-content\/uploads\/2020\/09\/image.png 1262w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group help-doc-toc\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<p class=\"wp-block-paragraph\">Table of Contents:<\/p>\n\n\n<nav aria-label=\"Table of Contents\" class=\"wp-block-table-of-contents\"><ol><li><a class=\"wp-block-table-of-contents__entry\" href=\"#org5f03a5f\">What&#039;s the Problem?<\/a><\/li><li><a class=\"wp-block-table-of-contents__entry\" href=\"#org7ebdd20\">Improvements to WordPress Core<\/a><\/li><li><a class=\"wp-block-table-of-contents__entry\" href=\"#org8ae8fe3\">Development is Easier with Local<\/a><\/li><li><a class=\"wp-block-table-of-contents__entry\" href=\"#howto-change-the-wp-environment-type-of-a-local-site\">How to change the WP_ENVIRONMENT_TYPE of a Local site<\/a><\/li><\/ol><\/nav>\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n<\/div><\/div>\n\n\n\n<div style=\"height:40px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading has-text-color\" id=\"org5f03a5f\" style=\"color:#000000\"><strong>What&#8217;s the Problem?<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When doing web development, you likely have found yourself in a situation where you wanted to have only certain things happening in certain environments.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, if you use Stripe on your website, you probably&nbsp;<strong>only<\/strong>&nbsp;want users to be charged when running in the production environment. When working on the staging, development, or Local environments, wouldn&#8217;t it be nice to use Stripe&#8217;s test data instead of real customer&#8217;s credit cards?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What about emails? When fiddling with a new mail sending feature, wouldn&#8217;t it be great to have&nbsp;<strong>all<\/strong>&nbsp;emails within the development environment go to your email as opposed to the end-user&#8217;s email?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There&#8217;s likely many other situations where you want only certain things happening in certain environments. Until recently, if you wanted that functionality, you had to create it on your own, and many popular plugins have implemented their own solutions:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/docs.woocommerce.com\/document\/subscriptions-handles-staging-sites\/\"><strong><span style=\"text-decoration: underline;\">How Does [WooCommerce] Subscriptions Handle Staging Sites and Migrations?<\/span><\/strong><\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/jetpack.com\/support\/staging-sites\/\"><strong><span style=\"text-decoration: underline;\">Jetpack Staging Sites<\/span><\/strong><\/a><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">It&#8217;s certainly clever how some of these solutions are implemented (See the code on Github for: <a href=\"https:\/\/github.com\/Automattic\/jetpack\/blob\/5faa321993c4ca955df871f5a77cd89fa411bca5\/packages\/status\/src\/class-status.php#L91\"><strong><span style=\"text-decoration: underline;\">jetpack<\/span><\/strong><\/a>&nbsp;and <a href=\"https:\/\/github.com\/wp-media\/wp-rocket\/blob\/c7a020448d6639ca3cb4cddf72195f9d9c18fbec\/inc\/functions\/api.php#L85\"><strong><span style=\"text-decoration: underline;\">wp-rocket<\/span><\/strong><\/a>), but ultimately it&#8217;s hard to share solutions when there isn&#8217;t one agreed-upon standard and every developer is having to re-invent the wheel.<\/p>\n\n\n\n<div style=\"height:40px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading has-text-color\" id=\"org7ebdd20\" style=\"color:#000000\"><strong>Improvements to WordPress Core<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">With the release of WordPress 5.5, WP Core includes the ability for developers to&nbsp;<a href=\"https:\/\/wordpress.org\/support\/wordpress-version\/version-5-5\/#defining-environments\"><strong><span style=\"text-decoration: underline;\">define a site&#8217;s environment type<\/span><\/strong><\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In particular, this release provides us with the&nbsp;<code>wp_get_environment_type()<\/code>&nbsp;function which returns one of four values:<\/p>\n\n\n\n<div class=\"wp-block-group has-light-gray-background-color has-background\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li><code>production<\/code>&nbsp;\u2013 this is the default. A site that is running live, connected to the internet and reachable on the internet.<\/li>\n\n\n\n<li><code>staging<\/code>&nbsp;\u2013 this is what you would use for staging environments, probably both connected to and reachable on the internet.<\/li>\n\n\n\n<li><code>development<\/code>&nbsp;\u2013 this is what you would use for development environments that are reachable on the internet, we automatically enable <code>WP_DEBUG<\/code>&nbsp;on environments where this is the environment type.<\/li>\n\n\n\n<li><code>local<\/code>&nbsp;\u2013 added in 5.5.1, this (usually development) environment can reach the internet but is not be reachable from the internet.<\/li>\n<\/ul>\n\n\n\n<p class=\"has-text-align-right has-light-gray-background-color has-background has-small-font-size wp-block-paragraph\">\u2013-&nbsp;<a href=\"https:\/\/make.wordpress.org\/core\/2020\/08\/27\/wordpress-environment-types\/\"><strong><span style=\"text-decoration: underline;\">https:\/\/make.wordpress.org\/core\/2020\/08\/27\/wordpress-environment-types\/<\/span><\/strong><\/a><\/p>\n<\/div><\/div>\n\n\n\n<div style=\"height:40px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading has-text-color\" id=\"org8ae8fe3\" style=\"color:#000000\"><strong>Development is Easier with Local<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This sort of clarity from WordPress core is awesome and allows plugin authors and site developers to have clear expectations about what capabilities an environment has!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Many plugins and development tools still need to be updated to make use of this functionality, but the most recent version of Local has all the configuration you need to program things only for <code>local<\/code> environments!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To get started:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Make sure you have the latest version of Local from the<strong> <span style=\"text-decoration: underline;\"><a rel=\"noreferrer noopener\" href=\"https:\/\/localwp.com\/releases\/\" target=\"_blank\">Releases page<\/a>.<\/span><\/strong><\/li>\n\n\n\n<li>Ensure that the version of WordPress Core is at least 5.5<\/li>\n\n\n\n<li>Add your custom code!<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Using the above space-cat example, you can hook into the <code>wp_head<\/code> action and output inline styles like this:<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">add_action('wp_head', 'local_space_cat');\nfunction local_space_cat() {\n    \/\/ Other possible values: 'production' 'staging' 'development'\n\tif ( 'local' === wp_get_environment_type() ) { ?&gt;\n\t\t&lt;style&gt;\n\t\t\tbody {\n\t\t\t\tbackground-image: url('space-cat-riding-unicorn.jpeg');\n\t\t\t}\n\t\t&lt;\/style&gt;\n\t&lt;?php }\n}<\/pre>\n\n\n<div id=\"help-step-block_b33bc41d00fd7c5814cada1a21d8c359\" class=\"help-note-container help-type-note\">\n\t<div class=\"help-icon\">\n\t\t<svg viewBox=\"0 0 512 512\" fill=\"currentColor\"><path fill=\"currentColor\" d=\"M256 512a256 256 0 1 1 0-512 256 256 0 1 1 0 512zm0-464a208 208 0 1 0 0 416 208 208 0 1 0 0-416zm70.7 121.9c7.8-10.7 22.8-13.1 33.5-5.3 10.7 7.8 13.1 22.8 5.3 33.5L243.4 366.1c-4.1 5.7-10.5 9.3-17.5 9.8-7 .5-13.9-2-18.8-6.9l-55.9-55.9c-9.4-9.4-9.4-24.6 0-33.9s24.6-9.4 33.9 0l36 36 105.6-145.2z\"><\/path><\/svg>\t<\/div>\n    <div class=\"help-text\">\n\t\t<p>There&#8217;s no need for you to add any other configuration to get this working in Local. Other remote environments may require adjustments to the <code>wp-config.php<\/code> file to return the correct value.<\/p>\n<p>If you&#8217;re curious, under the hood, when a site starts or is created, Local is automatically checking if a site has\u00a0the <code>WP_ENVIRONMENT_TYPE<\/code> constant set and defaulting it to <code>local<\/code> if it is missing &#8211; all so that you don&#8217;t have to.<\/p>\n\t<\/div>\n<\/div>\n\n\n\n<div style=\"height:40px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading has-text-color\" id=\"howto-change-the-wp-environment-type-of-a-local-site\" style=\"color:#000000\"><strong>How to change the WP_ENVIRONMENT_TYPE of a Local site<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">It&#8217;s great that Local, Flywheel, and WP Engine handle that configuration for you so that you can focus on making things, but what if you need to debug an issue where you need the Local site to think it&#8217;s a different environment type?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> What if you need to troubleshoot something that only shows up because of the <code><meta charset=\"utf-8\">WP_ENVIRONMENT_TYPE<\/code> constant is set to <strong>production<\/strong>?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In Local&#8217;s case, the <code>WP_ENVIRONMENT_TYPE<\/code> constant is set inside the site&#8217;s <code>wp-config.php<\/code> on your behalf. Changing this constant is as simple as opening the file and editing the value. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The site&#8217;s <code>wp-config.php<\/code> file lives in the <code>app\/public<\/code> directory.<\/p>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image size-large is-style-drop-shadow\"><img decoding=\"async\" width=\"1024\" height=\"568\" src=\"https:\/\/localwp.com\/wp-content\/uploads\/2023\/08\/WPEnvTypeScreenshot-1024x568.png\" alt=\"\" class=\"wp-image-2180\" srcset=\"https:\/\/localwp.com\/wp-content\/uploads\/2023\/08\/WPEnvTypeScreenshot-1024x568.png 1024w, https:\/\/localwp.com\/wp-content\/uploads\/2023\/08\/WPEnvTypeScreenshot-300x166.png 300w, https:\/\/localwp.com\/wp-content\/uploads\/2023\/08\/WPEnvTypeScreenshot-768x426.png 768w, https:\/\/localwp.com\/wp-content\/uploads\/2023\/08\/WPEnvTypeScreenshot.png 1132w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Knowing where that file is located is great and gives you a way to change the value for your Local site. Currently, this value is set on each site individually, and there is no way to globally set it for all sites; if you need to change the default value, we&#8217;d recommend using a Blueprint!<br><br>Again, the reason for using this field is to avoid unintended consequences &#8211; for example, having certain &#8220;production&#8221; actions run on your Local machine when you&#8217;re just trying to test things out. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Imagine an Ecommerce plugin that only processes payments in production. Unless there are other settings preventing the user from being charged, the Local site might end up directing a payment processor to charge the end-users which may mean multiple charges!<\/p>\n\n\n\n<div style=\"height:40px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-fe48e5de wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/community.localwp.com\/c\/support\/5\" target=\"_blank\" rel=\"noreferrer noopener\">Get Community Support<\/a><\/div>\n<\/div>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever found yourself in a situation where you needed to have unicorn riding space-cats appear only in your Local environment? Yeah, me too. Table of Contents: What&#8217;s the Problem? When doing web development, you likely have found yourself in a situation where you wanted to have only certain things happening in certain environments&#8230;.<\/p>\n","protected":false},"author":49,"parent":352,"menu_order":11,"template":"","tags":[43],"helpcategories":[5],"class_list":["post-263","help","type-help","status-publish","hentry","tag-advanced","helpcategories-advanced"],"acf":{"help_keywords":""},"_links":{"self":[{"href":"https:\/\/localwp.com\/wp-json\/wp\/v2\/help-doc\/263","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/localwp.com\/wp-json\/wp\/v2\/help-doc"}],"about":[{"href":"https:\/\/localwp.com\/wp-json\/wp\/v2\/types\/help"}],"author":[{"embeddable":true,"href":"https:\/\/localwp.com\/wp-json\/wp\/v2\/users\/49"}],"version-history":[{"count":0,"href":"https:\/\/localwp.com\/wp-json\/wp\/v2\/help-doc\/263\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/localwp.com\/wp-json\/wp\/v2\/help-doc\/352"}],"wp:attachment":[{"href":"https:\/\/localwp.com\/wp-json\/wp\/v2\/media?parent=263"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/localwp.com\/wp-json\/wp\/v2\/tags?post=263"},{"taxonomy":"helpcategories","embeddable":true,"href":"https:\/\/localwp.com\/wp-json\/wp\/v2\/helpcategories?post=263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}