How to exclude code for production environment?

I have a snippet op code I want not to be included in the production version of my page. Code is in base.php in this case. Something like:

<!--[if development]>
  <script>//Development only</script>
<![endif]-->

How to go about this?

You could try something like this (fast & easy way, but there may be a better way of doing it), just change the url to your dev one:

 if( $_SERVER['HTTP_HOST'] == 'dev.examle.com' ) {
  // show this on dev site

}

If you’re using Bedrock, you could check the WP_ENV constant.

We do exactly this for some bug tracking Javascript which we don’t want on our live sites.

As mentioned above, here’s what ours looks like:

<html>
    <head>
        <?php if(WP_ENV !== 'production') { ?>
            <script src="https://ourdebugtracker.fakeurl/debugger.js"></script>
        <?php } ?>
    </head>
3 Likes

If you’re not using Bedrock, you can define WP_ENV (or similar) in your wp-config.php file since that will different in each environment anyway. That’s what I do. Then you can use conditional checks as Chris described above. You can also use it for other WP settings directly within a shared config file and for stuff like GA tracking.

1 Like

This is a good quick and dirty solution! Thanks!