Use local image for default avatar

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.

Read More

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

Related posts

2 comments

  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.

  2. 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. 😛

Comments are closed.