Skip to content

Instantly share code, notes, and snippets.

@renakdup
Last active March 17, 2024 12:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save renakdup/36f4a8474d0cb13ecadf0393811d5330 to your computer and use it in GitHub Desktop.
Save renakdup/36f4a8474d0cb13ecadf0393811d5330 to your computer and use it in GitHub Desktop.
Adminpanel environment color for WordPress.
<?php
/**
* Adminpanel environment color for WordPress.
* It helps to highlight environments in color. Developers, Content Managers,
* and others will never confuse the environment where they work.
*
* Author: Andrei Pisarevskii
* Author Email: renakdup@gmail.com
* Author Site: https://wp-yoda.com/en/
*
* Version: 0.3
* Source Code: https://gist.github.com/renakdup/36f4a8474d0cb13ecadf0393811d5330
*
* Licence: MIT License
*/
namespace Renakdup\AdminpanelEnvColor;
AdminpanelEnvColor::init();
final class AdminpanelEnvColor {
public static function init() {
add_action( 'admin_head', [ __CLASS__, 'add_admin_bar_style' ] );
add_action( 'wp_head', [ __CLASS__, 'add_admin_bar_style' ] );
add_action( 'admin_bar_menu', [ __CLASS__, 'add_admin_bar_env_item' ], 100 );
}
public static function add_admin_bar_style() {
$adminpanel_colors = apply_filters(
'renakdup/adminpanel_env_color/colors',
[
'local' => null, // default wp color
'development' => '#2271b1', // blue
'staging' => '#cc6f00', // orange
'production' => '#6d0d0f', // red
]
);
// phpcs:disable WordPress.CodeAnalysis.AssignmentInCondition.Found, Squiz.PHP.DisallowMultipleAssignments.FoundInControlStructure, WordPress.Security.EscapeOutput.OutputNotEscaped
if ( $color = $adminpanel_colors[ wp_get_environment_type() ] ) {
echo '<style>
#wpadminbar { background-color: ' . $color . '!important; }
#adminmenu li.wp-has-current-submenu a.wp-has-current-submenu { background-color: ' . $color . '!important; }
</style>';
}
// phpcs:enable
echo '<style>.rd_adminpanel_env_color a {
box-shadow: inset 0 32px 5px rgba(0, 0, 0, 0.5) !important;
padding-left: 30px !important;
padding-right: 30px !important;
}
</style>';
}
/**
* @param $wp_admin_bar \WP_Admin_Bar
*
* @return void
*/
public static function add_admin_bar_env_item( $wp_admin_bar ) {
$args = [
'id' => 'rd_adminpanel_env_color',
'parent' => 'top-secondary',
'title' => 'ENV: ' . ucfirst( wp_get_environment_type() ),
'href' => '#',
'meta' => [
'class' => 'rd_adminpanel_env_color',
'title' => 'Your environment',
],
];
$wp_admin_bar->add_node( $args );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment