0

I'm trying to get the stylesheet of the currently active admin color scheme to style a plugin to fit into the color context.

As mentioned in this post I had a look at the implementation of admin_color_scheme_picker() in wp-admin/includes/misc.php:564. They use a global variable called $_wp_admin_css_colors but when I try to access (var_dump()) it, it's NULL.

Is there any way to access to actual CSS code?

Why is the global variable NULL? I did a grep -n '$_wp_admin_css_colors' -r . and couldn't find any line where it gets destroyed...

EDIT

I see it wasn't clear what I exactly want. I don't want to access the actual CSS code, but the color codes that gets parsed into CSS. In wp-includes/general-template.php:2117 the default themes are registered like this:

wp_admin_css_color( 'fresh', _x( 'Default', 'admin color scheme' ),
    admin_url( "css/colors$suffix.css" ),
    array( '#222', '#333', '#0074a2', '#2ea2cc' ),
    array( 'base' => '#999', 'focus' => '#2ea2cc', 'current' => '#fff' )
);

And I'd like to access the variables like this in PHP. Copying the definition is not an option, since other plugins and themes could add their theme themselves.

2

I actually made it!

In my script file, at the to I define a new variable and copy the $_wp_admin_css_colors variable into it using the admin_head hook, since the variable gets destroyed at some later point:

<?php
    global $admin_colors; // only needed if colors must be available in classes
    add_action('admin_head', function() {
        global $_wp_admin_css_colors;
        global $admin_colors; // only needed if colors must be available in classes
        $admin_colors = $_wp_admin_css_colors;
    });

    // more script content...
?>
1

Both answers are very helpful. Julian's answer grabs all the color schemes in play. tfrommen's answer finds the currently enabled one. If we combine both of those, we can go further and actually use it.

Here's an example that grabs the current color scheme and dynamically inserts a stylesheet with CSS variables corresponding to each color:

class MyTheme
    {
    static $ADMINCOLS = [];
    static function ImprintCols()
        {
        add_action('admin_head', function()
            {
            global $_wp_admin_css_colors; 
            if(!$_wp_admin_css_colors) return;
            $color = get_user_meta(get_current_user_id(), 'admin_color', true);
            if(key_exists($color,$_wp_admin_css_colors)) 
                self::$ADMINCOLS = $_wp_admin_css_colors[$color];
            });
        add_action('admin_footer', function()
            {   
            if(!self::$ADMINCOLS) return;
            $vars = [''];
            foreach(self::$ADMINCOLS->colors as $key=>$col)
                $vars[] = "\t--wp-admin-color-$key: $col;";
            if(isset(self::$ADMINCOLS->icon_colors)) 
                foreach(self::$ADMINCOLS->icon_colors as $key=>$col)
                    $vars[] = "\t--wp-admin-color-icon-$key: $col;";
            $vars[] = '';
            $styles = ":root {".join(PHP_EOL,$vars)."\t}";
            echo '<style id="wp-admin-cols">'.PHP_EOL.$styles.PHP_EOL.'</style>'.PHP_EOL;
            });
        }
MyTheme::ImprintCols();

This will create a style sheet declaration like the following (here I'm using the delectable Ectoplasm color scheme):

<style id="wp-admin-cols">
:root {
    --wp-admin-color-0: #413256;
    --wp-admin-color-1: #523f6d;
    --wp-admin-color-2: #a3b745;
    --wp-admin-color-3: #d46f15;
    --wp-admin-color-icon-base: #ece6f6;
    --wp-admin-color-icon-focus: #fff;
    --wp-admin-color-icon-current: #fff;
    }
</style>

Then any admin-based stylesheet can use these CSS variables, as so:

h1 { border-bottom: 2px solid var(--wp-admin-color-0); }
0

The color scheme is stored as user meta:

$color = get_user_meta(get_current_user_id(), 'admin_color', true);

However, the CSS file seems to be generated when having chosen another scheme.

So you just need this:

$admin_css_file = wp_admin_css_uri('css/colors');
1
  • 1
    Please re-read my answer, I clarified, what I really wanted :) – Julian F. Weinert Jan 23 '14 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.