Skip to content

Instantly share code, notes, and snippets.

@boswall
Last active September 17, 2020 12:27
Show Gist options
  • Save boswall/c50836f73088ffc46fbd0922fd37e412 to your computer and use it in GitHub Desktop.
Save boswall/c50836f73088ffc46fbd0922fd37e412 to your computer and use it in GitHub Desktop.
Add a notification to the admin top bar to show users if they are on the live / staging / test / dev version of a WordPress site
<?php
/*
Plugin Name: Site Environment Notification
Plugin URI: https://gist.github.com/boswall/c50836f73088ffc46fbd0922fd37e412
Description: Add a notification to the admin top bar to show users if they are on the ‘local’, ‘development’, ‘staging’ or ‘production’ version of the site. Use <code>define( 'WP_ENVIRONMENT_TYPE', 'local' );</code> to define this in the wp-config.php file.
Author: Matt Rose
Author URI: https://glaikit.co.uk/
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
add_action( 'admin_bar_menu', 'boswall_staging_notify_admin_bar_node', 999 );
function boswall_staging_notify_admin_bar_node( $wp_admin_bar ) {
if ( 'production' === wp_get_environment_type() ) {
return;
}
$args = array(
'id' => 'boswall-staging-notify',
'title' => strtoupper( wp_get_environment_type() ),
'meta' => array(
'class' => wp_get_environment_type(),
),
'parent' => 'top-secondary',
);
$wp_admin_bar->add_node( $args );
add_action( 'wp_after_admin_bar_render', 'boswall_staging_notify_admin_bar_styling' );
}
function boswall_staging_notify_admin_bar_styling() {
?>
<style media="screen">
#wpadminbar ul li#wp-admin-bar-boswall-staging-notify {
background-color: red;
}
#wpadminbar ul li#wp-admin-bar-boswall-staging-notify.development {
background-color: darkmagenta;
}
#wpadminbar ul li#wp-admin-bar-boswall-staging-notify.local {
background-color: darkgray;
}
</style>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment