# Environment Badge in the Admin Bar

> Shows a colored LOCAL, DEVELOPMENT, STAGING or PRODUCTION badge in the admin bar for administrators, based on the WP_ENVIRONMENT_TYPE setting.

- Type: PHP
- Runs: Run everywhere
- Category: [Admin](https://wpide.com/library/category/admin/)
- Tags: admin-bar, staging, environment
- Author: WPIDE Team
- Installs: 6
- Updated: 2026-10-28
- Page: https://wpide.com/library/environment-badge-in-admin-bar/

## Code

```php
add_action( 'admin_bar_menu', function ( $admin_bar ) {
    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }

    $environment = function_exists( 'wp_get_environment_type' ) ? wp_get_environment_type() : 'production';
    $colors      = array(
        'local'       => '#2271b1',
        'development' => '#2271b1',
        'staging'     => '#b26200',
        'production'  => '#d63638',
    );
    $color = isset( $colors[ $environment ] ) ? $colors[ $environment ] : '#50575e';

    $admin_bar->add_node( array(
        'id'     => 'wpide-environment',
        'parent' => 'top-secondary',
        'title'  => '<span style="background:' . esc_attr( $color ) . ';color:#fff;padding:3px 8px;border-radius:3px;font-weight:600;">' . esc_html( strtoupper( $environment ) ) . '</span>',
    ) );
}, 100 );
```

## Notes

Set the environment in wp-config.php, for example: define( 'WP_ENVIRONMENT_TYPE', 'staging' );

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Environment Badge in the Admin Bar" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
