How do I get the current environment to use in a function?

I’m using LogRocket for helping to support users, but I want to avoid calling it in non-production environments. I’ve set it up as a little plugin:

function add_logrocket() {
    echo '<script src="https://cdn.logrocket.io/LogRocket.min.js" crossorigin="anonymous"></script>';
    echo '<script>window.LogRocket && window.LogRocket.init("xxxxxx/xxxxxxx");</script>';
}
add_action( 'wp_enqueue_scripts', 'add_logrocket');

But this runs in all environments. Is there something I can use to check the environment? Like

if ( $env="production" ) {

}

WP_ENV is available in PHP

if (WP_ENV === 'production')
1 Like

Magic, thanks Ben! My best guess was $GLOBALS[ 'wp_env' ] which didn’t work…

1 Like