Wordpress - Alternative to using get_avatar function?
The get_avatar()
function applies a get_avatar
filter hook, that you can use to change the avatar markup:
return apply_filters('get_avatar', $avatar, $id_or_email, $size, $default, $alt);
I think this would be the correct way to hook into this filter:
function mytheme_get_avatar( $avatar ) {
$avatar = '<img src="<' . get_template_directory_uri() . '/images/authors/' . get_the_author_ID() . '.jpg" alt="' . get_the_author() . '">';
return $avatar;
}
add_filter( 'get_avatar', 'mytheme_get_avatar' );
EDIT
p.s. a nice alternative to this approach might be the Simple Local Avatars Plugin.
EDIT 2
The filter is applied using add_filter()
, not apply_filters()
. That was a typo on my part; fixed now!
EDIT 3
I don't think this is correct:
P.S: Just to clarify.. I replaced
get_avatar($tc->comment_author_email, $jmetc_options['avatar_size']);
withadd_filter('get_avatar', $avatar, $id_or_email, $size, $default, $alt);
First, you still call get_avatar()
in your template file, passing all the same parameters as previous. The add_filter()
call belongs in functions.php
.
Second, you can pass additional parameters to your filter function; e.g.:
function mytheme_get_avatar( $avatar, $id_or_email, $size ) {
$avatar = '<img src="<' . get_template_directory_uri() . '/images/authors/' . $id_or_email . '.jpg" alt="' . get_the_author() . '" width="' . $size . 'px" height="' . $size . 'px" />';
return $avatar;
}
add_filter( 'get_avatar', 'mytheme_get_avatar', 10, 3 );