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.
Opens WPIDE on
Code
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 add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.