3

I'm using the avatar_defaults filter hook to filter the default avatar list. For example:

function my_avatar_defaults( $avatar_defaults ) {
    $avatar_defaults['http://example.com/foo.png'] = __( 'Foo' );
    return $avatar_defaults;
}
add_filter( 'avatar_defaults', 'my_avatar_defaults' );

This works because if I visit Settings > Discussion and then scroll down, I can see Foo has been added as a default avatar option. See the following screenshot for an example:

Default avatar list of options

The problem is the src attribute of the image displayed next to Foo. It seems to be making a call to Gravatar. Here's an example of the source code I'm getting for the image next to Foo:

<img src="http://0.gravatar.com/avatar/efaeb0e0be9922051a1c4ccce766a141?s=32&d=http%3A%2F%2Fexample.com%2Ffoo.png%3Fs%3D32&r=G&forcedefault=1" />

How can I ensure the src attribute points to my image URL instead of the Gravatar URL (which seems to have my image's URL inside it)?

0

You are doing it correctly.

The call to gravatar.com passes the location of your custom image so that WP can load it. Take a look at the query string in the src, you'll see your image location. That's how it works.

  • 1
    Thanks for your reply. Why does WP need to load the image via Gravatar though? – henrywright Mar 5 '15 at 20:34
  • 1
    That's just the way it works. Gravatar is owned by Automattic, and is part of WordPress.com so that might have something to do with it: blog.gravatar.com/2013/04/03/… – butlerblog Mar 5 '15 at 20:37
  • 1
    Thanks for the info, I don't think calling Gravatar each time is ideal but as you say, it's just the way it works. – henrywright Mar 5 '15 at 21:28
1

Two notes:

  1. get_avatar() is a "pluggable" function. That means, that you can write a plugin to hold a new function named get_avatar(), which then will overwrite the original function used by WP.
  2. get_avatar() also has a filter

    return apply_filters( 'get_avatar', $avatar, $id_or_email, $size, $default, $alt );
    

    that you can use to change the return value:

    add_filter( 'get_avatar', function( $html, $id, $size, $default, $alt )
    {
        // apply some changes here
        return $html;
    }, 10, 5 );
    

    and that means that you can simply change the output as well and remove the src call to "Gravatar" so that it doesn't happen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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