0

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:

Thanks in advanced.

1 Answer 1

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' );
2
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Nov 8, 2021 at 8:06
  • preg_replacing the exact strings doesn't strike me as very clean. This is just the code from the second link in your question?
    – Rup
    Commented Nov 10, 2021 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.