Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I used a WP hack for displaying author's pics. For example, my single.php has an author slug which displays the author's pic.

I created a folder called authors in my theme/images folder. Based on the author's ID, I name the file 1.jpg, 2.jpg and so on.

So I call this image as

<img src="<?php bloginfo('template_directory') ?>/images/authors/<?php the_author_ID()?>.jpg" alt="<?php the_author(); ?>">

Now i'm modifying a plugin that displays the authors in the sidebar. However this plugin uses the get_avatar function, which is as follows:

/**
                 * If show avatar option is checked, add get_avatar function to cache.
                 */
                if($jmetc_options['show_avatar'] == 1) {
                    $jmevar['cache'] .= get_avatar($tc->comment_author_email, $jmetc_options['avatar_size']);
                }

Can someone advise me on how to use/modify the get_avatar in order to use the default code that I use?

share|improve this question
    
Thanks for the tip Chip.. i get a Unexpected '.' error in the function though –  Sledge81 Jul 12 '11 at 12:40
    
Ok, had to remove the ; after get_template_uri.. so now that its fixed.. still not able to see the changes made. I used apply_filters('get_avatar', $avatar, $id_or_email, $size, $default, $alt); replacing with get_avatar($tc and so on –  Sledge81 Jul 12 '11 at 13:02
    
You should comment in my answer, so I get notified when you comment. :) –  Chip Bennett Jul 12 '11 at 13:08
add comment

1 Answer

up vote 9 down vote accepted

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']); with add_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 );
share|improve this answer
    
Whoops! Updated the code to fix a couple small issues. I really must learn not to write code before my morning coffee... –  Chip Bennett Jul 12 '11 at 13:09
    
So I replaced apply_filters with add_filter but nothing changes. From firebug, the default gravatar url is still being pulled. <img width="60" height="60" class="avatar avatar-60 photo" src="http://1.gravatar.com/avatar/782b5a7a555f8147ac91d84c53e5dfa2?s=60&amp;d=ht‌​tp%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D60&am‌​p;r=G" alt=""> –  Sledge81 Jul 12 '11 at 13:13
    
P.S: Just to clarify.. I replaced get_avatar($tc->comment_author_email, $jmetc_options['avatar_size']); with add_filter('get_avatar', $avatar, $id_or_email, $size, $default, $alt); –  Sledge81 Jul 12 '11 at 13:14
    
See updated answer. Make sure you're still calling get_avatar() in your template file, and passing the necessary parameters to your filter function. –  Chip Bennett Jul 12 '11 at 13:55
1  
@ChipBennet Don't you have to state add_filter( 'get_avatar', 'mytheme_get_avatar', <whatever_priority>, 3 /* number of args! */ ); to actually get those arguments? –  Fronker Dec 12 '12 at 16:25
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.