3

I tried putting this snippet in my functions.php

add_filter( 'avatar_defaults', 'newgravatar' );

function newgravatar ($avatar_defaults) {
    $myavatar = get_bloginfo('template_directory') . '/images/default_avatar.png';
    $avatar_defaults[$myavatar] = "Locale";
    return $avatar_defaults;
}

A new avatar appears in Options-Discussion. Problem is it's not local at all.. the avatar is taken from http://i1.wp.com/mydomain.com/wp-content/themes/mytheme/images/default_avatar.png.

So it passes through i1.wp.com anyway.. how can I just have a friggin LOCAL link?

4
  • What does var_dump( get_bloginfo( 'template_directory' ) ); return? Also: consider using get_template_directory_uri() or get_template_stylesheet_uri() instead.
    – Pat J
    Dec 11, 2013 at 15:33
  • var_dump returns string(50) "http://website.com/wp-content/themes/mytheme" Dec 11, 2013 at 15:40
  • 1
    Hmmm. Have you tried disabling all plugins to see if one of them is causing this? I'm wondering if Jetpack (because when I surf to i1.wp.com in my browser, the favicon is the Jetpack logo) or any CDN plugins might be the cause.
    – Pat J
    Dec 11, 2013 at 15:45
  • I disabled all plugins (jetpack included). Cleared all caches (server, browser). No luck.. :( Dec 14, 2013 at 20:22

2 Answers 2

1

You have the Photon module enabled in the Jetpack plugin. That module routes your images through WordPress.com's CDN. If you don't want this to happen, disable the Photon module in Jetpack.

5
  • Nice catch!! ;) Dec 11, 2013 at 20:31
  • I talked too early.. actually photon is disabled.. could it be it messed anyway with some WP function? Dec 11, 2013 at 20:33
  • Uhm... I even tried disabling Jetpack completely.. no luck! Dec 12, 2013 at 2:17
  • Are you using a caching plugin of some kind? Clear the cache.
    – Otto
    Dec 12, 2013 at 2:33
  • No plugin either :( Dec 14, 2013 at 20:19
1

I've been looking up how to do this myself and that same function shown above everyone keeps suggesting isn't working, perhaps due to a recent update to WP's codebase, I'm not entirely sure. It's a little hackish and it involves editing core files (not good) but it's a work-around solution. Here's the steps:

  1. Use the same function specified in the original post
  2. Open /wp-includes/pluggable.php and search for "function get_avatar", this should get you to where the function is defined in the file
  3. Scroll down to just above where the first IMG tag is outputted, you should see this:

    $out = str_replace( '&', '&', esc_url( $out ) );
    
    $avatar = "<img alt='{$safe_alt}' src='{$out}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
    
  4. Just under $out, add $out = $default; This basically passes the default image source url (stored in the options table)

So it should look like this:

    $out = str_replace( '&#038;', '&amp;', esc_url( $out ) );
    $out = $default;

    $avatar = "<img alt='{$safe_alt}' src='{$out}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
} else {
    $avatar = "<img alt='{$safe_alt}' src='{$default}' class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />";
}

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

Again, this isn't ideal obviously because the next time you update WordPress, you'll lose this change. But for the moment it works.

To future visitors looking to not modify core files, you may want to look into filtering the WP function get_avatar. It seems to do so would require modifying the string supplied in the filter with a regular expression to remove the gravatar domain. I attempted it but suck at regex. :P

2
  • Thanks!!! Would be great if someone could maybe create a snippet to put on functions.php to replicate this. Mar 18, 2014 at 18:45
  • You're very welcome. I'm glad my time spent doing trial and error could be useful. Like I mentioned, it seems like very possible to use one function instead of editing WP's core by using regular expressions to filter out the gravatar nonsense passed by the get_avatar filter. Maybe someone more gifted with PHP can lend a hand on that. Ideally this would be submitted as a bug in the WP code trac (I think I read somewhere it already is, not sure though). Mar 18, 2014 at 21:27

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.