I am trying to change default Gravatar size. Since I am using WP-generated comments template (using comments_template() function), I turned to filters and this is my custom code:

function change_avatar_size ( $id_or_email, $size = 100, $default = '', $alt = '', $args = null ) {

    $args['size']    = (int) $size;  

    $url2x = get_avatar_url( $id_or_email, array_merge( $args, array( 'size' => $args['size'] * 2 ) ) );

    $args = get_avatar_data( $id_or_email, $args );

    $url = $args['url']; 

    $class = array( 'avatar', 'avatar-' . (int) $args['size'], 'photo' );  

    $avatar = sprintf(
        "<img alt='%s' src='%s' srcset='%s' class='%s' height='%d' width='%d' %s/>",
        esc_attr( $args['alt'] ),
        esc_url( $url ),
        esc_attr( "$url2x 2x" ),
        esc_attr( join( ' ', $class ) ),
        (int) $args['height'],
        (int) $args['width'],
        $args['extra_attr']
    );

    return $avatar;

}
add_filter( 'get_avatar', 'change_avatar_size' );

The size does change, but I am not getting the correct Gravatar URL. I am getting default Gravatar image actually, as if the generated link is not correct. Any ideas?

Here's how to do it using the method Bruno went with (for others wondering this):

wp_list_comments( array(
    'avatar_size' => 32
) );

You can also do it this way depending on your use case:

global $comment;
echo get_avatar($comment, 64)
  • Exactly, I should have added that answer instead of only posting a link to WordPress reference. Thanks for an update :) – Bruno Kos Dec 3 '15 at 12:44

Found it - no need to bother with filters, given that there is avatar_size parameter within wp_list_comments, it can be found here: https://developer.wordpress.org/reference/functions/wp_list_comments/

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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