Snippet for own (gr)avatars

Following snippet used(and works great) but generates a redirect chain :(.

If (function_excists('basicgravatar') 

 function basicgravatar ($avatar_defaults) {
   $myavatar = get_stylesheet_directory_uri() . '/images/gravatar.png'; //Child theme
   //$myavatar = get_bloginfo('template_directory') . '/images/gravatar.png';  //Main theme
   $avatar_defaults[$myavatar] = "New default avatar";
   return $avatar_defaults; }

add_filter( 'avatar_defaults', 'basicgravatar' );

Using GTmetrix which shows following:

Read More

Remove the following redirect chain if possible:
http://0.gravatar.com/avatar/?d=http://oursite.com/media/wp-content/themes/our-child-theme/images/gravatar.png&s=40
and 2 more lines with almost identical msg.

Also this is shown as result after using code above:

The following resources have identical contents, but are served from different URLs. Serve these resources from a consistent URL to save 2 request(s) and 20.2KiB.

The more comments the more redirects.
(The quotes above show results after only two(2) comments and 1 author box, imagine 50 or more comments…)..how to solve this, without editing any core file.

Thanks in advance for time and effort.

Related posts

Leave a Reply

2 comments

  1. This is essentially how gravatar service works.

    Your code has no knowledge if particular email has gravatar associated with it. The only way would be to query gravatar API and store if there is gravatar available (and re-query for updates) for each email and resulting functionality will quickly snowball past just having that redirect there.

    A little more practical tweak might be implementing lazy loading for gravatars, so they aren’t loaded until visitor actually scrolls to them.

  2. I use FV Gravatar Cache plugin which solves this problem by:

    • caching gravatars with WordPress cron job..
    • caching gravatars on comment submission
    • maintaining a single copy of the default gravatar instead of
      downloading it again and again for all the email addresses with no
      gravatar associated