Using WordPress ‘admin_color_scheme_picker’ PHP action

The admin_color_scheme_picker WordPress PHP action fires in the ‘Admin Color Scheme’ section of the user editing screen. The section is only enabled if a callback is hooked to the action, and if there is more than one defined color scheme for the admin.

Usage

add_action('admin_color_scheme_picker', 'your_custom_function');
function your_custom_function($user_id) {
    // your custom code here
}

Parameters

  • $user_id: int – The user ID.

More information

See WordPress Developer Resources: admin_color_scheme_picker

Examples

Add a custom admin color scheme

Register a new admin color scheme and add it to the ‘Admin Color Scheme’ picker.

// Register a custom admin color scheme
function register_custom_admin_color_scheme() {
    wp_admin_css_color(
        'custom_scheme', // unique identifier
        __('Custom Scheme', 'domain'), // display name
        get_stylesheet_directory_uri() . '/custom_scheme_colors.css', // CSS file
        array('#1d2327', '#2c3338', '#3e4d59', '#ffffff') // base colors
    );
}
add_action('admin_init', 'register_custom_admin_color_scheme');

// Add the custom color scheme to the picker
function add_custom_scheme_picker($user_id) {
    global $_wp_admin_css_colors;
    $color_scheme = get_user_meta($user_id, 'admin_color', true);
    if (empty($color_scheme)) {
        $color_scheme = 'fresh';
    }
    ?>
    <fieldset>
    <legend class="screen-reader-text"><span><?php _e('Admin Color Scheme', 'domain'); ?></span></legend>
    <?php
    foreach ($_wp_admin_css_colors as $id => $color) {
        ?>
        <div class="color-option">
            <input name="admin_color" type="radio" value="<?php echo esc_attr($id); ?>" id="admin_color_<?php echo esc_attr($id); ?>" <?php checked($color_scheme, $id); ?>>
            <label for="admin_color_<?php echo esc_attr($id); ?>"><?php echo esc_html($color->name); ?></label>
            <table class="color-palette">
                <tr>
                    <?php
                    foreach ($color->colors as $html_color) {
                        ?>
                        <td style="background-color: <?php echo esc_attr($html_color); ?>;">&nbsp;</td>
                        <?php
                    }
                    ?>
                </tr>
            </table>
        </div>
        <?php
    }
    ?>
    </fieldset>
    <?php
}
add_action('admin_color_scheme_picker', 'add_custom_scheme_picker');

Remove a color scheme from the picker

Remove the ‘Ectoplasm’ color scheme from the ‘Admin Color Scheme’ picker.

function remove_ectoplasm_color_scheme($user_id) {
    global $_wp_admin_css_colors;
    unset($_wp_admin_css_colors['ectoplasm']);
}
add_action('admin_color_scheme_picker', 'remove_ectoplasm_color_scheme');

Remove all color schemes except the default

Remove all color schemes except the default ‘Fresh’ color scheme from the ‘Admin Color Scheme’ picker.

function remove_other_color_schemes($user_id) {
    global $_wp_admin_css_colors;
    $_wp_admin_css_colors = array('fresh' => $_wp_admin_css_colors['fresh']);
}
add_action('admin_color_scheme_picker', 'remove_other_color_schemes');

Add a custom color scheme based on user role

Add a custom color scheme for ‘Editor’ user role and display it in the ‘Admin Color Scheme’ picker only for users with the ‘Editor’ role.

// Register a custom admin color scheme for editors
function register_editor_admin_color_scheme() {
    wp_admin_css_color(
        'editor_scheme',
        __('Editor Scheme', 'domain'),
        get_stylesheet_directory_uri() . '/editor_scheme_colors.css',
        array('#23282d', '#383c40', '#4d5e6f', '#ffffff')
    );
}
add_action('admin_init', 'register_editor_admin_color_scheme');

// Add the custom color scheme to the picker for editors
function add_editor_scheme_picker($user_id) {
    if (user_can($user_id, 'editor')) {
        global $_wp_admin_css_colors;
        $color_scheme = get_user_meta($user_id, 'admin_color', true);
        if (empty($color_scheme)) {
            $color_scheme = 'fresh';
        }
        ?>
        <fieldset>
        <legend class="screen-reader-text"><span><?php _e('Admin Color Scheme', 'domain'); ?></span></legend>
        <?php
        foreach ($_wp_admin_css_colors as $id => $color) {
            if ('editor_scheme' === $id) {
                ?>
                <div class="color-option">
                    <input name="admin_color" type="radio" value="<?php echo esc_attr($id); ?>" id="admin_color_<?php echo esc_attr($id); ?>" <?php checked($color_scheme, $id); ?>>
                    <label for="admin_color_<?php echo esc_attr($id); ?>"><?php echo esc_html($color->name); ?></label>
                    <table class="color-palette">
                        <tr>
                            <?php
                            foreach ($color->colors as $html_color) {
                                ?>
                                <td style="background-color: <?php echo esc_attr($html_color); ?>;">&nbsp;</td>
                                <?php
                            }
                            ?>
                        </tr>
                    </table>
                </div>
                <?php
            }
        }
        ?>
        </fieldset>
        <?php
    }
}
add_action('admin_color_scheme_picker', 'add_editor_scheme_picker');

Force a specific color scheme for a user role

Force the ‘Midnight’ color scheme for all users with the ‘Author’ role.

function force_midnight_scheme_for_authors($user_id) {
    if (user_can($user_id, 'author')) {
        update_user_meta($user_id, 'admin_color', 'midnight');
    }
}
add_action('admin_color_scheme_picker', 'force_midnight_scheme_for_authors');