Last active
April 9, 2024 17:10
-
-
Save mathetos/11257001ef3842d5f5d5877f1a0e3f3d to your computer and use it in GitHub Desktop.
Get User Admin Color Schemes for UI styling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Add Current Users Admin Color Scheme | |
// 1. Create helper function to grab individual colors easily | |
// 2. Add individual colors to document root as CSS variables | |
function wpptpcv_return_admin_colors() { | |
global $_wp_admin_css_colors; | |
// Get the user's selected color scheme name | |
$color_name = get_user_option('admin_color'); | |
// Assuming that $_wp_admin_css_colors is a dictionary with color schemes | |
// and each color scheme has a 'colors' attribute | |
$admin_colors = $_wp_admin_css_colors; | |
// Get the colors for the selected color scheme | |
$colors = $admin_colors[$color_name]->colors; | |
// Return the colors as an array | |
return $colors; | |
} | |
// Add colors as CSS variables | |
// Some schemes only have 3 colors, so limiting to that for now | |
function wpptpcv_custom_admin_styles() { | |
$colors = wpptpcv_return_admin_colors(); | |
?> | |
<style id="wpptpvc-admin-color-scheme-variables"> | |
:root { | |
--color1: <?php echo $colors[1] ?>; | |
--color2: <?php echo $colors[2] ?>; | |
--color3: <?php echo $colors[3] ?>; | |
} | |
</style> | |
<?php | |
} | |
add_action('admin_head', 'wpptpcv_return_admin_colors'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment