Get current active wp color scheme

The question:

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?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

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.

Method 2

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
        )
    )
)

Method 3

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

Method 4

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment