jovilog
jovilog

Reputation: 111

Remove color scheme options from user's profile page in WordPress 6.0

I've always used this code in a must-use plugin to remove the whole color schemes section:

remove_action( 'admin_color_scheme_picker', 'admin_color_scheme_picker' );

Unfortunately, with WordPress 6.0 this does not work anymore. I've found that Core's add_action( 'admin_color_... was recently moved from default-filters.php file to the admin-filters.php file, but I am unsure why and how I'd have to update the above snippet to make it work again.

Upvotes: 5

Views: 2108

Answers (3)

Barry Ceelen
Barry Ceelen

Reputation: 161

For a remove_action() call to be effective, it needs to be called after the action you want to remove has been added, and before the action runs.

WordPress adds the admin_color_scheme_picker action in admin-filters.php and then runs the action in the user-edit.php admin page template.

To remove the admin_color_scheme_picker action right before it is called on the user profile page, you can run the remove_action() call using the admin_head-profile.php hook:

add_action( 'admin_head-profile.php', 'wpse_72463738_remove_admin_color_scheme_picker' );

/**
 * Remove the color picker from the user profile admin page.
 */
function wpse_72463738_remove_admin_color_scheme_picker() {
    remove_action( 'admin_color_scheme_picker', 'admin_color_scheme_picker' );
}

Note that the admin_head-{$hook_suffix} hook fires in the head section for a specific admin page. In the example above replacing $hook_suffix with profile.php in the hook name makes it run on the user admin profile page.

Upvotes: 9

RafaSashi
RafaSashi

Reputation: 17205

In addition to Barry Ceelen's answer if you want to remove admin_color_scheme_picker for both profile.php and user-edit.php screens you can do:

add_filter('admin_head',function($class){

        $screen = get_current_screen();
        
        if( in_array($screen->id,array(
        
            'profile',
            'user-edit',
        
        ))){

            // remove color picker
            
            remove_action( 'admin_color_scheme_picker', 'admin_color_scheme_picker');
        }
});

Upvotes: 2

Josh Bonnick
Josh Bonnick

Reputation: 2813

You can use the other part of the if statement in user-edit.php to remove the ability to change the color scheme.

From user-edit.php

<?php if ( count( $_wp_admin_css_colors ) > 1 && has_action('admin_color_scheme_picker' ) ) : ?>

Although not a direct solution to using the remove action function, you can set the $_wp_admin_css_colors global to an empty array...

add_action( 'admin_init', function () {
    global $_wp_admin_css_colors;
    $_wp_admin_css_colors = [];
} );

Upvotes: 4

Related Questions