Make WordPress Core

Opened 8 years ago

Last modified 3 years ago

#37773 new enhancement

Update get_avatar() to support native and registered image sizes.

Reported by: jmichaelward's profile jmichaelward Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 2.5
Component: Media Keywords: has-patch has-unit-tests needs-testing reporter-feedback dev-feedback
Focuses: Cc:


The get_avatar() method currently allows developers to retrieve a user's avatar of a particular size by passing in an integer value, such as get_avatar( '', 80 ).

In some cases, it might be useful to instead retrieve an avatar by passing in an image size registered with WordPress (e.g., thumbnail, medium, large).

See attached patch.

Attachments (3)

patch.diff (1.7 KB) - added by jmichaelward 8 years ago.
37773.1.diff (2.7 KB) - added by jmichaelward 8 years ago.
37773.2.diff (3.6 KB) - added by jmichaelward 7 years ago.

Download all attachments as: .zip

Change History (14)

8 years ago

#1 @jmichaelward
8 years ago

  • Keywords has-patch added

#2 @SergeyBiryukov
8 years ago

  • Keywords needs-unit-tests added

8 years ago

#3 @jmichaelward
8 years ago

  • Keywords has-unit-tests added; needs-unit-tests removed

Patch 37773.1.diff includes unit tests, as well as refined logic in get_avatar based on the findings from those tests. :)

#4 @swissspidy
7 years ago

  • Version 4.7 deleted

This ticket was mentioned in Slack in #core-media by desrosj. View the logs.

7 years ago

#6 @desrosj
7 years ago

  • Keywords needs-testing reporter-feedback added
  • Version set to 2.5

@jmichaelward I like the idea. In talking it over with @joemcgill, we were wonderin what would happen if someone supplies a non-square image size? If I recall correctly (and it has not changed), I believe Gravatar will only return a square. Say the image size is 800x400. The image returned could return unexpected results.

#7 @desrosj
7 years ago

  • Keywords dev-feedback added

The pre_get_avatar filter could also be used for changing the size requested. While it may not be as convenient as expanding the function, this is definitely possible.

Last edited 7 years ago by desrosj (previous) (diff)

#8 @jmichaelward
7 years ago

@desrosj That's a good question. I can't believe it's been almost a year since I first submitted this - let me take a look at my submitted patch and investigate what happens with non-square image sizes and see if I can address it adequately.

#9 @jmichaelward
7 years ago

@desrosj So, first, a little background on this ticket: I was working on some updates for a plugin developed by my last employer that allows users to upload a custom user profile photo. We had written a custom method that users would call to output that image, and I'd felt that seemed unnecessary when we can simply filter get_avatar inside of our plugin, and instruct them to use the native WordPress method inside of their themes (which also means they will not receive fatal errors when deactivating the plugin, should they have called the method without a function_exists check).

With regard to Gravatar's square imagery, it appears that I had accounted for the square imagery returned by the service. In the patch, if a user requests a non-square WordPress image size (e.g., medium, large, or something custom), get_avatar will check if the requested size is registered, get the register image size's dimensions, then set the size to the greater of height or width.

I see now that this solution is half-baked. The user may not be expecting a square image, and they may not be expecting the larger of the two dimensions to be used. I do see that get_avatar sets some defaults for height and width, so your recommendation for pre_get_avatar may already address this.

In my view, a call to get_avatar should:

  1. Allow the user to retrieve an image from non-Gravatar services (for example, the WordPress media library)
  2. Allow them to have a non-square profile image

I'm going to play around with this code some more and see if I can address this using pre_get_avatar, per your recommendation, and then also check whether it's possible to support passing a string for the image size as my previous patch does.

#10 @jmichaelward
7 years ago

After some further consideration, I think it may be acceptable to allow a user to pass a registered WordPress image size and have it revert to a square Gravatar image if pre_get_avatar is not filtered, so long as we document what developers can expect in this case. This latest patch will set the size of the Gravatar image to the height of the registered WordPress image size (squared) in the event that pre_get_avatar is not filtered.

These changes will allow developers to filter pre_get_avatar to return non-square avatars using native WordPress registered image sizes. It accounts for custom image sizes as well, so if a theme registers its own avatar thumbnail, the developers can call <?php get_avatar( $user_id, 'my-custom-avatar-thumbnail' ); ?> and expect to get the correct registered image size.

Additional unit tests are needed, but I think this is on the right path.

7 years ago

This ticket was mentioned in Slack in #core-media by antpb. View the logs.

3 years ago

Note: See TracTickets for help on using tickets.