简体   繁体   中英

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.

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 = [];
} );

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.
 */
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM