users - WordPress 5.8 - Hide or Remove personal fields from admin Profile page

admin2025-01-07  3

There are several threads on this topic, but none updated to Wordpress 5.8

The question is, in 2021 WP 5.8, what is the cleanest or recommended code to hide profile fields when creating/editing a user, except the basic ones (username,email,name,lastname...)?

When I say "recommended" or "cleanest" I mean for example if there is something that can be removed "via hook", like the "Admin colour scheme", then this would be the best option, right?

Some options for other wordpress versions:

  • /

  • Remove Personal Options section from Profile

Thanks in advanced.

There are several threads on this topic, but none updated to Wordpress 5.8

The question is, in 2021 WP 5.8, what is the cleanest or recommended code to hide profile fields when creating/editing a user, except the basic ones (username,email,name,lastname...)?

When I say "recommended" or "cleanest" I mean for example if there is something that can be removed "via hook", like the "Admin colour scheme", then this would be the best option, right?

Some options for other wordpress versions:

  • https://isabelcastillo.com/hide-personal-options-wordpress-admin-profile

  • https://www.majas-lapu-izstrade.lv/how-to-remove-wordpress-admin-profile-page-fields-including-personal-options-biographical-info-website-etc-and-titles-without-js/

  • Remove Personal Options section from Profile

Thanks in advanced.

Share Improve this question asked Nov 7, 2021 at 12:48 MaskplainMaskplain 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0
  • This is working for me in 5.8.
  • Works in "adding new user" and in "edit user".
  • "Public display name" managed with a plugin.

The code:

// Remove fields from Admin profile page
if ( ! function_exists( 'cor_remove_personal_options' ) ) {
    function cor_remove_personal_options( $subject ) {
        $subject = preg_replace('#<h2>'.__("Personal Options").'</h2>#s', '', $subject, 1); // Remove the "Personal Options" title
        $subject = preg_replace('#<tr class="user-rich-editing-wrap(.*?)</tr>#s', '', $subject, 1); // Remove the "Visual Editor" field
        $subject = preg_replace('#<tr class="user-comment-shortcuts-wrap(.*?)</tr>#s', '', $subject, 1); // Remove the "Keyboard Shortcuts" field
        $subject = preg_replace('#<tr class="show-admin-bar(.*?)</tr>#s', '', $subject, 1); // Remove the "Toolbar" field
        $subject = preg_replace('#<h2>'.__("Name").'</h2>#s', '', $subject, 1); // Remove the "Name" title
        // $subject = preg_replace('#<tr class="user-display-name-wrap(.*?)</tr>#s', '', $subject, 1); // "Display name publicly as" field (not working ok when editing user. Better remove and manage with "Force First and Last Name as Display Name" plugin
        $subject = preg_replace('#<h2>'.__("Contact Info").'</h2>#s', '', $subject, 1); // Remove the "Contact Info" title
        $subject = preg_replace('#<tr class="user-url-wrap(.*?)</tr>#s', '', $subject, 1); // Remove the "Website" field in EDIT USER
        $subject = preg_replace('#<th scope="row"><label for="url(.*?)</th>#s', '', $subject, 1); // Remove the "Website" field in NEW USER (part 1)
        $subject = preg_replace('#<td><input name="url"(.*?)</td>#s', '', $subject, 1); // Remove the "Website" field SUKINOZ in NEW USER (part 2)  
        $subject = preg_replace('#<h2>'.__("About Yourself").'</h2>#s', '', $subject, 1); // Remove the "About Yourself" title
        $subject = preg_replace('#<h2>'.__("About the user").'</h2>#s', '', $subject, 1); // Remove the "About the user" title      
        $subject = preg_replace('#<tr class="user-description-wrap(.*?)</tr>#s', '', $subject, 1); // Remove the "Biographical Info" field
        $subject = preg_replace('#<tr class="user-profile-picture(.*?)</tr>#s', '', $subject, 1); // Remove the "Profile Picture" field
        return $subject;
    }

    function cor_profile_subject_start() {
        if ( ! current_user_can('manage_options') ) {
            ob_start( 'cor_remove_personal_options' );
        }
    }

    function cor_profile_subject_end() {
        if ( ! current_user_can('manage_options') ) {
            ob_end_flush();
        }
    }
}
add_action( 'admin_head', 'cor_profile_subject_start' );
add_action( 'admin_footer', 'cor_profile_subject_end' );

//Remove fields from Admin profile page via JS to hide nickname field which is mandatory
function remove_personal_options(){
    if ( ! current_user_can('manage_options') ) { // 'update_core' may be more appropriate
        echo '<script type="text/javascript">jQuery(document).ready(function($) {
            $(\'form#your-profile tr.user-nickname-wrap\').hide(); // Hide the "nickname" field
        });</script>';
    }
}
add_action('admin_head','remove_personal_options');

// Removes ability to change Theme color for the users
remove_action( 'admin_color_scheme_picker', 'admin_color_scheme_picker' );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736258045a484.html

最新回复(0)