How to change Gravatar size when using comments_template (ok)

https://wordpress.stackexchange.com/questions/195196/how-to-change-gravatar-size-when-using-comments-template

Asked 5 years, 9 months agoActive 3 years agoViewed 1k times11

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' );

2 Answers

ActiveOldestVotes3

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)

Last updated