Halloween Sale 15% off Ends in See the plans
PHP

Environment Name in the Browser Tab Title

Prefixes browser tab titles with [LOCAL], [DEV] or [STAGING] for logged-in users on non-production sites, in the admin and on the front end, so open tabs are easy to tell apart.

Don’t have WPIDE yet? Install it on your site

Code

$wpide_env_label = function () {
    $labels      = array( 'local' => 'LOCAL', 'development' => 'DEV', 'staging' => 'STAGING' );
    $environment = wp_get_environment_type();

    return isset( $labels[ $environment ] ) && is_user_logged_in() ? '[' . $labels[ $environment ] . '] ' : '';
};

add_filter( 'admin_title', function ( $title ) use ( $wpide_env_label ) {
    return $wpide_env_label() . $title;
}, 99 );

add_filter( 'document_title_parts', function ( $parts ) use ( $wpide_env_label ) {
    if ( $wpide_env_label() && isset( $parts['title'] ) ) {
        $parts['title'] = $wpide_env_label() . $parts['title'];
    }
    return $parts;
}, 99 );

Notes

Visitors and the production site see normal titles.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.