plugin development - Remove profile picture option (and other things) from profile.php (in admin)

admin 2025-06-05   2

Just upgraded to WordPress 4.4 and I notice now my users' profile page in admin, there's a section called "profile picture", with a link to Gravatar.

The plugin I'm developing restricts a lot of things in profile.php; unfortunately by deleting from the DOM via jQuery:

jQuery( document ).ready(function() {
    jQuery( "h2:contains('Personal Options')" ).remove();
    jQuery( "h2:contains('Name')" ).remove();
    jQuery( "h2:contains('Contact Info')" ).remove();
    jQuery( "h2:contains('About Yourself')" ).remove();
    jQuery( "h2:contains('Account Management')" ).remove();
    jQuery( "tr.show-admin-bar" ).parents("table:first").remove();
    jQuery( "tr.user-first-name-wrap" ).remove();
    jQuery( "tr.user-last-name-wrap" ).remove();
    jQuery( "tr.user-nickname-wrap" ).remove();
    jQuery( "tr.user-display-name-wrap" ).remove();
    jQuery( "tr.user-url-wrap" ).remove();
    jQuery( "tr.user-description-wrap").parents("table:first").remove();
    jQuery( ".user-sessions-wrap" ).remove();
});

Before I continue on the jQuery addiction, is there a better way to do this (aside from output buffer replacement)? Also, is there some other setting specifically for the user profile picture, or is this jQuery solution going to be the answer for now?

Just upgraded to WordPress 4.4 and I notice now my users' profile page in admin, there's a section called "profile picture", with a link to Gravatar.

The plugin I'm developing restricts a lot of things in profile.php; unfortunately by deleting from the DOM via jQuery:

jQuery( document ).ready(function() {
    jQuery( "h2:contains('Personal Options')" ).remove();
    jQuery( "h2:contains('Name')" ).remove();
    jQuery( "h2:contains('Contact Info')" ).remove();
    jQuery( "h2:contains('About Yourself')" ).remove();
    jQuery( "h2:contains('Account Management')" ).remove();
    jQuery( "tr.show-admin-bar" ).parents("table:first").remove();
    jQuery( "tr.user-first-name-wrap" ).remove();
    jQuery( "tr.user-last-name-wrap" ).remove();
    jQuery( "tr.user-nickname-wrap" ).remove();
    jQuery( "tr.user-display-name-wrap" ).remove();
    jQuery( "tr.user-url-wrap" ).remove();
    jQuery( "tr.user-description-wrap").parents("table:first").remove();
    jQuery( ".user-sessions-wrap" ).remove();
});

Before I continue on the jQuery addiction, is there a better way to do this (aside from output buffer replacement)? Also, is there some other setting specifically for the user profile picture, or is this jQuery solution going to be the answer for now?

Share Improve this question edited Dec 12, 2015 at 19:37 C C asked Dec 12, 2015 at 18:58 C CC C 1,96818 silver badges31 bronze badges 1
  • There are some hooks to remove certain parts of the profile page. Additionally you can just use php to buffer the page output and then strip the relevant content. See my answer below for more details. – Phill Healey Commented Dec 24, 2018 at 13:03
Add a comment  | 

4 Answers 4

Reset to default 8

We can use the show_avatars option to remove the Profile Picture section.

  • We can visit /wp-admin/options.php to turn it off manually.

  • Or update it with:

    update_option( 'show_avatars', 0 );
    
  • Or modify it through a filter:

    add_filter( 'option_show_avatars', '__return_false' );
    
  • We could also restrict it to the profile.php page:

    add_action( 'load-profile.php', function()
    {
       add_filter( 'option_show_avatars', '__return_false' );
    } );
    

There are some hooks to remove certain parts of the profile page. Additionally you can just use php to buffer the page output and then strip the relevant content.

// Remove fields from Admin profile page
if (!function_exists('CCK_remove_profile_options')) {
function CCK_remove_profile_options($subject)
{
    $subject = preg_replace('#<h2>' . __("Personal Options") . '</h2>#s', '', $subject, 1); // Remove the "Personal Options" title
    $subject = preg_replace('#<tr class="user-syntax-highlighting-wrap(.*?)</tr>#s', '', $subject, 1); // Remove the "Syntax Highlighting" field
    $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); // Remove the "Display name publicly as" field
    $subject = preg_replace('#<h2>' . __("Contact Info") . '</h2>#s', '<h2>' . __("Login Management") . '</h2>', $subject, 1); // Remove the "Contact Info" title
    $subject = preg_replace('#<tr class="user-url-wrap(.*?)</tr>#s', '', $subject, 1); // Remove the "Website" field
    $subject = preg_replace('#<tr class="user-googleplus-wrap(.*?)</tr>#s', '', $subject, 1); // Remove the "Google+" field
    $subject = preg_replace('#<tr class="user-twitter-wrap(.*?)</tr>#s', '', $subject, 1); // Remove the "Website" field
    $subject = preg_replace('#<tr class="user-facebook-wrap(.*?)</tr>#s', '', $subject, 1); // Remove the "Website" field
    $subject = preg_replace('#<h2>' . __("Account Management") . '</h2>#s', '', $subject, 1); // Remove the "About Yourself" title
    $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 Yourself" 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

    //$subject = preg_replace('#<h3>' . __("User Expiry Information") . '</h3>#s', '', $subject, 1); // Remove the "Expire Users Data" title
    return $subject;
}

function CCK_profile_subject_start()
{
    //if ( /*!*/ current_user_can('manage_options') ) {
    ob_start('CCK_remove_profile_options');
    //}
}

function CCK_profile_subject_end()
{
    //if ( /*!*/ current_user_can('manage_options') ) {
    ob_end_flush();
    //}
}
}
add_action('admin_head', 'CCK_profile_subject_start');
add_action('admin_footer', 'CCK_profile_subject_end');
// Removes ability to change Theme color for the users
remove_action('admin_color_scheme_picker', 'admin_color_scheme_picker'); 

Deleting things from DOM is not truly deleting them. If all you want is better GUI then it can be a valid technique but if you want to actually prevent people from messing around with those fields (lets say for security reason) then it is not enough.

Maybe a bigger problem with your code is that you try to identify text which means that the plugin will fail for non english installs and sometimes even for english ones.

Next problem is that other plugins might add their own fields leaving the page with a strange display.

If you need a very tightly controlled profile page then you should write one and let user access only it. The built-in profile page is not flexible enough right now to easily add/remove or change permissions for its various components (this is a general statement, some components are more flexible then others).

Here is simple solutions for that, add more ids to hide more fields

<script type="text/javascript">/* <![CDATA[ */
var hideFields = [ "aim", "yim", "jabber" ];
jQuery.each( jQuery( "form#your-profile tr" ), function() {
    var field = jQuery( this ).find( "input,textarea,select" ).attr( "id" );
    if ( hideFields.indexOf( field ) != -1 ) {
        jQuery( this ).remove();
    }
});
/* ]]> */</script>

Or you could use Hide Profile fields plugin

转载请注明原文地址: http://conceptsofalgorithm.com/Algorithm/1749071066a316105.html

最新回复 (0)