4

Is there a way to return the current active color scheme?

This is how to register a new admin color scheme

<?php register_admin_color_schemes(); ?> 

is there something like get_admin_color_scheme(); that will return which is active?

3

The current admin color scheme is a user setting, you can get its value with:

$current_color = get_user_option( 'admin_color' );

See the function admin_color_scheme_picker() in wp-admin/includes/misc.php for an usage example.

  • 1
    Is there a possibility to actually GET the styles? The implementation in wp-admin/includes/misc.php uses a global variable called $_wp_admin_css_colors which is null when I try to access it... – Julian F. Weinert Jan 23 '14 at 15:46
4

If you want to access the colors in PHP, you have to do a little trick. In the file from toschos answer they use the global variable $_wp_admin_css_colors. This gets destroyed at any point of the runtime.

I actually found the right hook to copy it:

<?php
    $admin_colors;
    add_action('admin_head', function(){global $_wp_admin_css_colors; $admin_colors = $_wp_admin_css_colors;});
?>

So now I can access the colors at any point in the script I want like this:

$admin_colors[get_user_option('admin_color')]['colors']; // array(0 => #222, 1 => #333, 2 => #0074a2, 3 => #2ea2cc)

Here is an excerpt of $admin_colors to demonstrate the structure:

$admin_colors = Array
(
    [fresh] => stdClass Object
    (
        [name] => Default
        [url] => https://example.com/wordpress/wp-admin/css/colors.min.css
        [colors] => Array
        (
            [0] => #222
            [1] => #333
            [2] => #0074a2
            [3] => #2ea2cc
        )
        [icon_colors] => Array
        (
            [base] => #999
            [focus] => #2ea2cc
            [current] => #fff
        )
    )
)
  • What if we want to add a new color scheme combination to the list and not simply access the list. Is this possible? – klewis Nov 3 '16 at 15:57
  • 1
    I'm not particular sure, but I think WP has an API for registering color schemes. But it might work, though, if you change $_wp_admin_css_colors first. – Julian F. Weinert Nov 3 '16 at 21:55
  • You probably try an earlier hook as well, as this only exports, as the admin header gets loaded and thus could be too late – Julian F. Weinert Nov 3 '16 at 21:56
  • That was it. I didn't know the function existed. Thanks! – klewis Nov 3 '16 at 21:58
  • 1
    My apologies. The wp_admin_css_color() function helps us extend the color scheme base that comes with Wordpress. We can create our own color schemes easily with that function. I had a couple developers help me out with that here - wordpress.stackexchange.com/questions/244982/… – klewis Nov 4 '16 at 14:16
2

Every user can set his own admin color scheme, so you can also get the color scheme according to an user ID:

get_user_option( 'admin_color', get_current_user_id() );

Further reading: WP 3.8 Default Admin Colour For All Users

  • 1
    You don’t need get_current_user_id(), that’s the default. – fuxia Dec 23 '13 at 2:45
0

I think this is the simple way to get the current admin color scheme or set it also, see the code:

Set the default admin color scheme:

<?php

/**
 * Set the default admin color scheme for WordPress user.
 */
add_filter('get_user_option_admin_color', 'set_default_admin_color');
function set_default_admin_color($result)
{

    // set new default admin color scheme
    $result = 'midnight';

    // return the new default color
    return $result;

}

Get the current admin color scheme:

/**
 * Get the current admin color scheme from WordPress user.
 */
add_filter('get_user_option_admin_color', 'get_current_admin_color');
function get_current_admin_color($result)
{

    global $_wp_admin_css_colors;

    // get current admin color scheme name
    $current_color_scheme = $result;

    // get all available colors from scheme name
    $colors = $_wp_admin_css_colors[$current_color_scheme];

    // now you can use this colors or store it
    // var_dump($colors);

    // important: we should return the default color scheme
    return $result;

 }

Get all available admin colors schemes:

/**
 * Get all available admin colors schemes from WordPress user.
 */
add_filter('get_user_option_admin_color', 'get_all_admin_colors');
function get_all_admin_colors($result)
{

    global $_wp_admin_css_colors;

    // get all available color schemes
    $colors = $_wp_admin_css_colors;

    // now you can use this color schemes or store it
    // var_dump($colors);

    // important: we should return the default color scheme
    return $result;

}

@see gist here also: https://gist.github.com/mohamdio/bb9a1a18446aca257935846d217060e9

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.