Alternative to using get_avatar function?

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.

Read More

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?

Related posts

Leave a Reply

2 comments

  1. 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 );
    
  2. There is a small error in above’s function

    better use this:

    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 );