2

How to remove the Gravatar image from Username column in the All User admin page?

enter image description here

4 Answers 4

1

Since there is no special avatar column to unset (the avatars are inside the username column), you could try instead to hide the avatars via css:

function hide_avatars_wpse_94126() {
    if(!current_user_can('manage_options')){
        // hide only for non-admins
        echo "<style>.users td.username img.avatar{display:none !important;}</style>";
    }
}
add_action('admin_head-users.php','hide_avatars_wpse_94126');

where they are hidden for non-admins.

The result will be like this:

Hide avatars

1
  • Thanks for the answer. But I just found a filter for the get_avatar function that seems to work better (see my answer).
    – Giraldi
    Apr 2, 2013 at 3:47
1
  1. You have to filter pre_option_show_avatars and return something that evaluates to FALSE but isn't FALSE. Let's say a 0.
  2. You should restrict that filter to user list pages.
    • In a single-site the proper action is restrict_manage_users.
    • In a network management screen that could be restrict_manage_users-network, but that doesn't work, so we use the filter wpmu_users_columns and return whatever we get here.

Result:

add_filter( 'wpmu_users_columns', 'no_avatars_in_user_list' );
add_action( 'restrict_manage_users', 'no_avatars_in_user_list' );

function no_avatars_in_user_list( $in = NULL )
{
    add_filter( 'pre_option_show_avatars', '__return_zero' );
    return $in;
}
4
  • Not sure if i did it correctly but this didn't seem to do anything or me. Anyway, I already found a better filter for this. See my answer. Thanks for the answer, though.
    – Giraldi
    Apr 2, 2013 at 3:50
  • @GiraldiMaggio I tested my code on single site and multi site.
    – fuxia
    Apr 2, 2013 at 4:05
  • I gave it another try, copied & pasted it, but the avatars still show.
    – Giraldi
    Apr 2, 2013 at 4:10
  • Thanks, toscho. Your solution worked well for me in 3.5.1. I run a site with a lot of users and this really sped things up for me while preserving the avatar function elsewhere. Seems to verify, at least in my case, that evaluating to false does the trick. Appreciate your help!
    – sojweb
    May 5, 2013 at 16:04
1

There seems to be a filter for the get_avatar function. So I just output an empty string to it.

function remove_avatar_from_users_list( $avatar ) {
    if (is_admin()) {
        global $current_screen; 
        if ( $current_screen->base == 'users' ) {
            $avatar = '';
        }
    }
    return $avatar;
}
add_filter( 'get_avatar', 'remove_avatar_from_users_list' );

UPDATE: Restrict to 'All Users' page only.

2
  • That would affect many more places: admin bar for example or plugin pages. Plus, this filter runs after get_avatar() has done all the expensive work. Way too late.
    – fuxia
    Apr 2, 2013 at 3:49
  • Okay, I restricted it to 'All Users' page only. In regards to the 'late' issue, I still haven't figured that one out yet.
    – Giraldi
    Apr 2, 2013 at 4:06
1

This is an old thread, but just in case someone else needs it: starting in version 4.2, you can use the pre_get_avatar filter to bypass pulling the actual avatar and just sending back an empty string.

add_filter( 'pre_get_avatar', 'rkv_remove_avatar_from_list', 10, 3 );
/**
 * Remove the avatar from just the user list.
 *
 * @param string $avatar       HTML for the user's avatar. Default null.
 * @param mixed  $id_or_email  The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
 *                             user email, WP_User object, WP_Post object, or WP_Comment object.
 * @param array  $args         Arguments passed to get_avatar_url(), after processing.
 *
 * @return string              An empty string.
 */
function rkv_remove_avatar_from_list( $avatar, $id_or_email, $args ) {

    // Do our normal thing on non-admin or our screen function is missing.
    if ( ! is_admin() || ! function_exists( 'get_current_screen' ) ) {
        return $avatar;
    }

    // Get my current screen.
    $screen = get_current_screen();

    // Bail without the object.
    if ( empty( $screen ) || ! is_object( $screen ) || empty( $screen->base ) || 'users' !== $screen->base ) {
        return $avatar;
    }

    // Return an empty string.
    return '';
}

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.