Show the environment in the admin bar
Adds a coloured “Staging”, “Development” or “Local” label to the admin bar on sites that aren’t live, so nobody edits the wrong copy of the site.
<?php
add_action(
'admin_bar_menu',
static function ( $bar ) {
$type = wp_get_environment_type();
if ( 'production' === $type || ! current_user_can( 'edit_posts' ) ) {
return;
}
$bar->add_node(
array(
'id' => 'snipfire-environment',
'parent' => 'top-secondary',
'title' => esc_html( ucfirst( $type ) ),
'meta' => array( 'class' => 'snipfire-env snipfire-env--' . sanitize_html_class( $type ) ),
)
);
},
1
);
$snipfire_env_css = static function () {
if ( is_admin_bar_showing() && 'production' !== wp_get_environment_type() ) {
echo '<style>#wpadminbar .snipfire-env>.ab-item{background:#b32d2e;color:#fff;font-weight:600}#wpadminbar .snipfire-env--staging>.ab-item{background:#dba617;color:#1d2327}</style>';
}
};
add_action( 'wp_head', $snipfire_env_css );
add_action( 'admin_head', $snipfire_env_css );
Good to know
WordPress reads the environment from WP_ENVIRONMENT_TYPE in wp-config.php. Many hosts set it on their staging copies.
In these packs
- More admin tweaks: Useful columns, a tidier admin bar, a welcome box for clients, and fewer menus for editors.
- Developer tools: Maintenance mode, template names, page stats, environment labels and a WP-Cron viewer.