I've been trying to set a different width and height using the get_avatar function. I need to set the author's avatar in single.php to 60x40 size.

So let's say the gravatar looks like this:

enter image description here

when set to 60x40, it would look like this (resized and cropped):

enter image description here

However, the default get_avatar does not seem to allow different values for width and height, since

<?php echo get_avatar( $comment, '60' ); ?>

would simply result in 60x60-sized gravatar.

I'm not sure if this is a nice way for doing this, but I tried adding this to functions.php, by facilitating the TimThumb image resizer (I renamed the timthumb.php to display.php):

function change_avatar_url($urel) {
    $urel = str_replace("src='", "src='". bloginfo( 'template_directory' ) ."/script/display.php?src=", $urel);
    $urel = str_replace("' class", "&w=60&h=40&zc=1' class", $urel);
    return $urel;

but it (seem obviously) does not work.

Is there any way to achieve this?

3 Answers 3


Unfortunately at moment Gravatar service itself only accepts single number for size and only serves square images.

So you have to achieve it with CSS or download, modify and cache images.

  • Which isn't that stupid, as gravatar requests normally slow down pretty much.
    – kaiser
    Commented Oct 15, 2012 at 21:15
  • That's too bad. Thanks for the answer. Managed to get a work-around but not sure if it's the best.
    – deathlock
    Commented Oct 16, 2012 at 3:32

As informed by @Rarst, apparently currently Gravatar only accepts one value for size. It is really unfortunate. However I managed to work-around this by facilitating timthumb.php and a function I found from here: How to get gravatar url alone

I'm not sure if this is the best way to do this (it looks messy), however this works for me for creating 60x40 px size. It doesn't seem to work well when I tried with other sizes, though. Not sure why.

Well, here goes.

First I add gravatar.com into the list of allowed sites in timthumb.php (I renamed the file to display.php). The list is under the $ALLOWED_SITES.

Then in functions.php I put this (notice that I rename the timthumb.php to display.php):

// Get the gravatar URL
// source: https://wordpress.stackexchange.com/questions/46904/how-to-get-gravatar-url-alone
function get_gravatar_url( $email ) {
    $hash = md5( strtolower( trim ( $email ) ) );
    return 'http://gravatar.com/avatar/' . $hash;

// Function to display the custom-sized gravatar
function custom_gravatar_timthumb($width, $height, $class) {
    $custom = get_template_directory_uri() . "/script/display.php?src=". get_gravatar_url(get_the_author_meta('email')) ."w=". $width ."&h=". $height ."&zc=1&a=c";
    echo "<img src='" . $custom . "' class='". $class ."' alt='avatar' />";

Then in single.php (where I display the gravatar) I put this:

custom_gravatar_timthumb(60, 40, "author-avatar avatar photo");
apply_filters('get_avatar', $avatar, $id_or_email, $size, $default, $alt);

So you can filter the output:

function wpse69318_avatar_sizes( $avatar, $id_or_email, $size, $default, $alt )
    if ( is_single() )
        return preg_replace( '/width\=\"[0-9]{1,3}\"/i', 'width="40px"', $avatar );

    // return default for other pages
    return $avatar
add_filter( 'get_avatar', 'wpse69318_avatar_sizes', 10, 5 );
  • I'm sorry, I forget to mention that I'm trying to make the image cropped (the reason why I tried with TimThumb). With preg_replace, wouldn't the gravatar be resized and looked weird?
    – deathlock
    Commented Oct 15, 2012 at 20:32
  • Why don't you simply specify it in CSS and then hide the stuff with overflow: hidden?
    – kaiser
    Commented Oct 15, 2012 at 20:43
  • *Correction: "With preg_replace, wouldn't the gravatar only be resized without getting cropped, thus made it looks weird?" For example it would look like this: i.imgur.com/B5eKh.jpg Meanwhile I'm trying to make it look like this: i.sstatic.net/UlhBW.jpg Sorry for my bad grammar..
    – deathlock
    Commented Oct 15, 2012 at 20:46
  • Hmm, you're right, it's possible to do it with CSS too... but I would also like to know if it is possible to do this by modifying the gravatar output. Can't think a good example right now, but perhaps in case of displaying certain size for author's bio?
    – deathlock
    Commented Oct 15, 2012 at 20:49

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.