Customize WordPress Avatar Using Filter Hook

WordPress have an excellent feature for providing individual thumbnail within your WordPress blog. However, most of us are aware that most users who have a customize thumbnail image are usually those who sign up with Gravatar and the rest of the user will only have their default image provided by WordPress. Some of us will want a customize WordPress Avatar for our logged in user or customize the default WordPress image rather than using the default one. This is possible using filter hook that is currently not documented in WordPress to provide a customize avatar for individual logged in user. On the other hand, default image can also be overwrite so that you can have your own set of avatar uniquely for your WordPress blog.

Default WordPress Avatar

Rather than using WordPress default avatar, there is an easy way to customize the default WordPress Avatar even without using any filter hook. Here is how you do it.

<?php echo get_avatar( $comment->comment_author_email, 100, get_template_directory_uri()."/images/face.jpg" ); ?>

Notice that get_avatar is the recommended way to get avatar from WordPress and this is usually located in your WordPress theme. This function allows you to change the default WordPress avatar by providing a customize path to the default WordPress avatar. You can also create your own set of random avatar using this method. (either using another table in your database or just use the php method random to randomly select an image)

Customize WordPress Avatar With Filter Hook

Another way of customizing WordPress Avatar is to use a filter hook provided by WordPress. I found this accidentally while customizing WordPress for one of my project needs. The filter hook that was given within the method of get_avatar was this.

			add_filter('get_avatar', array($this,'site_get_avatar'), 10, 5);

Notice that the new filter was get_avatar which is also the method given in WordPress to display any avatar. Take note that i also provide the third and fourth parameter. The fourth parameter is important so that our method site_get_avatar have the appropriate value for us to work with, without the fourth parameter, you won't be able to retrieve other information other than the avatar given by WordPress itself. You will understand this better after i have provided the method site_get_avatar as shown below,

function site_get_avatar($avatar, $id_or_email, $size, $default, $alt){
	//$avatar format includes the tag <img>
	$imgpath = "http://website.com/image/myownimage.jpg";
	$my_avatar = "<img src='".$path."' alt='".$alt."' height='".$size."' width='".$size."' />";
	return $my_avatar;
}

The above method contains 5 parameters which are describe below,

  1. $avatar: the avatar given by WordPress with the tag . This is not the path of the default image.
  2. $id_or_email: this is the email or id of the user
  3. $size: size of the image
  4. $default: this should be the default image provided by WordPress or the path given by us when we used get_avatar (third parameter as shown previously)
  5. $alt: this is the alt value given in your img tag.

Previously, i mention that the fourth parameter in the filter hook is important right? The reason is that if the fourth parameter is not provided in your filter hook, the value for number 2 onwards will not be provided by WordPress to your custom method, site_get_avatar. You will only get the value $avatar in default. Hence, the fourth parameter is important 🙂

WordPress Customize Avatar

Depending on what you need, this action hook is actually quite useful for customizing WordPress avatar. You can abandon this hook if you just want to customize the default image provided by WordPress. However, if you wish to customize each user avatar without using Gravatar, you will definitely find this pretty useful! Good luck!

One thought on “Customize WordPress Avatar Using Filter Hook

Comments are closed.