WordPress: Manage your avatar localy

For few days, my avatar on this blog, is no longer the usual gray and white figure. Without original idea, I use the logo of the blog. For the moment, I don’t want to use avatar services such as Gravatar. I found a filter to replace the default avatar image, with my own image, stored locally.

The trick consist in using the filter get_avatar, applied by WordPress, at the end of the function of the same name. This function is stored in the file wp-admin/pluggable.php.

This filter has 5 parameters

  • $avatar: the HTML code generated by the function get_avatar,
  • $id_or_email: ID or email address from the one we want to display the avatar,
  • $size: size of the image,
  • $default: the image used by default, if no avatar is found,
  • $alt: texte used in the attribute alt of the image.

In a first step, you have to create an avatar, and store it in an area that WordPress can reach. Most simple is to choose the directory (or a sub-directory) of your theme.

The PHP code of the function is the following:

function my_get_avatar( $avatar, $id_or_email='', $size = '96', $default = '', $alt = false) {
             $email_list  = array( 'xxx@yyy', 'www@zzz');
             $avatar_url = get_bloginfo('template_url').'my_avatar.png';
	$mail = FALSE;
	if (is_object($id_or_email)) 
		$mail = $id_or_email->comment_author_email;
	elseif (is_string($id_or_email) || is_numeric($id_or_email))
		$mail = $id_or_email;
	if ($mail !== FALSE) {
		if (array_search($mail, $email_list) !== FALSE ) {	
			$avatar = '<img src="'.$avatar_url.'" class="avatar avatar-'.$size.' photo" height="'.$size.'" width="'.$size.'" />';
	return ($avatar);

This function looks for an image named my_avatar_img.png, in the root of the current theme. Replace the line $avatar_url = ... with your own url.

Then, we just have to « connect » this function to WordPress:

add_filter('get_avatar', ' my_get_avatar', 10, 5);

This small trick can be use only with single author blog, because we just associate a list of addresses with a single image. If your blog is used by several authors, you should modify the function. This modification is not complex, but I think, in this case, it should be better to use avatar services, to avoid additional support and maintenance tasks.