22

I have to remove some profile fields from the 'Profile' page according to the user roles. For removing the color picker I used remove_action("admin_color_scheme_picker", "admin_color_scheme_picker"). I want to remove these fields:

  1. Nickname
  2. Display name publicly as
  3. AIM, Yahoo, Google talk and About yourself fields
  4. The 'Show Admin Bar' section along with the 2 checkboxes

I went through the file 'user-edit.php' to see if there are any actions/filters that allow me to do so, but it seems there aren't. Can anyone tell me how to do this? Any help would be appreciated. Thanks

EDIT: Thanks @helenhousandi for the answer. I had already did it this way though.

<?php
add_action('admin_footer-profile.php', 'remove_profile_fields');
function remove_profile_fields()
{
    if(current_user_can('custom_role'))
    { ?>
        <script type="text/javascript">
            jQuery("h3:contains('Personal Options')").next('.form-table').remove();
            jQuery("h3:contains('Personal Options')").remove();
            jQuery("h3:contains('About Yourself')").next('.form-table').remove();
            jQuery("h3:contains('About Yourself')").remove();
        </script>
<?php }
}
?>

I know this is a bit longer, but it worked! I think I'll go with @helenhousandi's answer.

5
  • Where should this go? functions.php?
    – Fabian
    Commented Jan 22, 2016 at 13:16
  • Yes. It can go to functions.php or a plugin file. As long as the hooks are right, it will work from either of the places. Commented Jan 22, 2016 at 16:46
  • Ok, but if i put it in the functions.php, how do I return the javascript? with return or echo?
    – Fabian
    Commented Jan 25, 2016 at 15:31
  • 1
    It seems that since WordPress v4.4.1, Nickname has become a required field and gives ERROR: Please Enter A Nickname when updating a user with this hidden but not populated, so you can't just hide it any more
    – icc97
    Commented Apr 6, 2016 at 22:08
  • It's 2022 and profile sections are still not wrapped with a unique ID.. oh well.
    – mike23
    Commented Aug 16, 2022 at 10:52

7 Answers 7

20

For the contact methods filter: user_contactmethods:

function update_contact_methods( $contactmethods ) {

    unset( $contactmethods['aim'] );
    unset( $contactmethods['jabber'] );
    unset( $contactmethods['yim'] );

    return $contactmethods;

}
add_filter( 'user_contactmethods', 'update_contact_methods' );
3
  • Thanks... got this one! Anything for the 'Show Admin Bar' and 'About yourself' sections? Commented Nov 3, 2011 at 11:48
  • Please update this if you find a solution... I'm accepting your answer though. Thanks! Commented Nov 3, 2011 at 14:35
  • +1 because as far as I can recall removing fields via PHP also stops WordPress from creating blank entries in the usermeta table.
    – Ashfame
    Commented Apr 9, 2012 at 13:12
10

Though the user_contactmethods filter is the one of the few for the user profile screen, since the other fields are not required in WordPress, you could use jQuery's .hide() and .remove() to effectively get rid of the ones you don't want without complications in terms of saving the information. Sure, a person without JS on would still see them, but that's a small minority and I'm guessing that the information changing there won't actually affect anything.

An example of removing the whole personal options piece (show admin bar and color scheme). I know it's not what you're looking for exactly, just wanted to give you a quick example I have on hand:

// remove personal options block
if( is_admin() ){
    remove_action( 'admin_color_scheme_picker', 'admin_color_scheme_picker' );
    add_action( 'personal_options', 'prefix_hide_personal_options' );
}

function prefix_hide_personal_options() {
  ?>
    <script type="text/javascript">
        jQuery( document ).ready(function( $ ){
            $( '#your-profile .form-table:first, #your-profile h3:first' ).remove();
        } );
    </script>
  <?php
}

Also, a note about the Show Admin Bar option: in 3.3 the admin bar is becoming the header and thus cannot be turned on or off, so it may not be worth worrying about.

I updated the code example to be more accurate and complete. Had some leftovers in there.

4
  • Thanks! This is a nice solution! What I did was something similar but I used a different action hook so that my script runs only on profile.php. I also needed to do this for a specific custom role defined in my system. Updating my question with the answer. Commented Nov 12, 2011 at 11:10
  • 1
    If you add the following to the jQuery it will also hide the Nickname and 'Display Name as' from the question: $("#nickname,#display_name").parent().parent().remove();
    – icc97
    Commented Feb 1, 2012 at 8:16
  • You can also use edit_user_profile hook. And not use the jQuery( document ).ready method to run the code instantaneously. Like this
    – Giraldi
    Commented May 30, 2012 at 7:24
  • 1
    Nickname is required now since WP v.4.4.1
    – icc97
    Commented Apr 6, 2016 at 22:09
4

As there seems to be no good php hook for this I ended up hiding the fields with CSS then remove them with JS.

add_action( 'admin_head', 'remove_default_profile_fields' );

function remove_default_profile_fields() {

    global $pagenow;

    if( 'profile.php' != $pagenow) return;

    remove_action( 'admin_color_scheme_picker', 'admin_color_scheme_picker' );

    // <tr> selectors, each containing a field
    $tr = array(
        "tr.user-rich-editing-wrap",
        "tr.user-comment-shortcuts-wrap",
        "tr.user-first-name-wrap",
        "tr.user-last-name-wrap",
        "tr.user-admin-bar-front-wrap",
        "tr.user-profile-picture",
        "tr.user-user-login-wrap",
        "tr.user-display-name-wrap",
        "h2" // Personally I decided to remove all H2 tags too.
    );

    $selectors = implode(", ", $tr);

    // Hide the fields with css, so even if javascript is disabled they wont show up. 
    echo "<style>{$selectors}{display:none;}</style>"; ?>

    <script type="text/javascript">
        jQuery( document ).ready(function( $ ){
           // Remove selected <tr>'s
           $( '<?= $selectors; ?>' ).remove();
           // Remove any empty table that may have been left over
           $(".form-table:not(:has(tr))").remove();
        });
    </script>

  <?php
}
2

BE CAREFUL!!

1) Removing a field may not be good, as on update, if you wont send the essential fields to WORDPRESS update hook, then it might set empty values to those fields! (for example, in submitting form, there should exists i.e. <input name="username".../>)
I Think, that it's better to HIDE from users visually only, with jquery .hide(); and not with .remove();
EXAMPLE code to hide "DISPLAY NAME" and "NICKNAME" fields:

jQuery(document).ready(function( $ ){
    $("#nickname,#display_name").parent().parent().hide();
});

2) To hide admin bar, there is a special option in wordpress menu (in Settings). for custom code, like i.e.

add_action('after_setup_theme', 'remove_admin_bar');

function remove_admin_bar()
{
  if (!current_user_can('administrator') && !is_admin()) 
  {
    show_admin_bar(false);
  }
}
1

From the wordpress support forum you can do the removal using PHP. This removes the About Yourself section:

if (! function_exists('remove_plain_bio') ){
    function remove_plain_bio($buffer) {
        $titles = array('#<h3>About Yourself</h3>#','#<h3>About the user</h3>#');
        $buffer=preg_replace($titles,'<h3>Password</h3>',$buffer,1);
        $biotable='#<h3>Password</h3>.+?<table.+?/tr>#s';
        $buffer=preg_replace($biotable,'<h3>Password</h3> <table class="form-table">',$buffer,1);
        return $buffer;
    }

    function profile_admin_buffer_start() { ob_start("remove_plain_bio"); }

    function profile_admin_buffer_end() { ob_end_flush(); }
}
add_action('admin_head', 'profile_admin_buffer_start');
add_action('admin_footer', 'profile_admin_buffer_end');

The code replaces the 'About Yourself' header with 'Password' and then strips out anything between that and the next table it finds (which is the password table).

Also the plugin WP Hide Dashboard does similar jQuery replacing if you want to extend that.

1
  • For future reference, you might wish to use admin_head-profile.php and admin_footer-profile.php instead
    – user5424
    Commented Mar 18, 2012 at 14:28
0

Use

add_action('admin_head', 'remove_profile_fields');

and add

jQuery(document).ready(function(e) {

before JavaScript.

1
  • A little explanation would be great!
    – bravokeyl
    Commented Sep 22, 2016 at 7:13
-5

There's no filter to disable them. Look at the source (/wp-admin/user-edit.php) when you need to do stuff like this. You could hide them with JS but they can still be made visible and values are updated on POST.

Only viable option is to hack the core yourself. Just make sure you handle the update and disregard these fields on POST.

3
  • Yes, I checked that file already! The JS solution doesn't seem too secure. I'll see what I can do! Thanks! Commented Nov 3, 2011 at 14:34
  • 5
    If the only solution you can think of is to hack core, then perhaps you shouldn't answer that particular question. You also don't need to suggest to the question asker to look at the file already mentioned, and you are also not correct in stating that there are no filters at all - surely you must have read the question and earlier answer first? Commented Nov 3, 2011 at 14:53
  • @helenhousandi xoxo Commented Nov 3, 2011 at 14:58

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.