WordPress.org

Ready to get started?Download WordPress

Forums

[closed] PROTIP: get_avatar usage for theme authors (22 posts)

  1. Samuel Wood (Otto)
    Tech Ninja
    Posted 4 years ago #

    Theme authors: I've seen a lot of this type of code in some themes.

    echo get_avatar( get_comment_author_email(), '60' );

    That code is wrong. You should have used this instead:

    echo get_avatar( $comment, '60' );

    get_avatar is a relatively smart function. If you pass it an email address, then it gets the avatar for that email address. But, if you pass it a whole comment object, then it knows how to deal with that properly too. And you really want to pass it the whole object.

    Why? Because get_avatar is not limited to getting just gravatars. A plugin might hook a filter in, and recognize when it might be appropriate to get a different sort of avatar, such as one from Twitter or Facebook or some such thing. And if you just passed the email, then the plugin can't see the whole comment, including that oh-so-useful comment-meta.

    So do us all a favor. Pass in the whole object. Don't try to be all clever and pass in only the email address. Let the upstream code take care of that for you.

  2. burtonlancaster
    Member
    Posted 4 years ago #

    THANK YOU! Spent the last 4 hours trying to figure out why fb avatar was showing up in admin panel but not on post comment ... theme author had this ...

    get_avatar( $id_or_email, $size = '70', $default = '<path_to_url>' ); ?>
  3. whiteknightuk
    Member
    Posted 4 years ago #

    On one of my themes that Woothemes made they have some how hardcoded it so it uses Gravatars all the time, I have tryed using what you said but it isn't the normal comment code its etra in a theme-comment.php, would you be able to look into it?

    the code for theme-comments.php is:

    ...
    function the_commenter_avatar() {
        $email = get_comment_author_email();
        $avatar = str_replace( "class='avatar", "class='photo avatar", get_avatar( "$email", "32" ) );
        echo $avatar;
    }
    ...
    ?>

    edited by mod to only leave the relevant bits

  4. Samuel Wood (Otto)
    Tech Ninja
    Posted 4 years ago #

    Change that function to this:

    function the_commenter_avatar() {
        global $comment;
        $avatar = str_replace( "class='avatar", "class='photo avatar", get_avatar( $comment, "32" ) );
        echo $avatar;
    }
  5. whiteknightuk
    Member
    Posted 4 years ago #

    Hi Otto, thanks for the support, that hasnt worked. Now if I leave a comment using facebook it went to white screen and now wrecks the style of the page and also still shows gravatar.

    http://www.pspcentre.co.uk/mercedes-benz-sls-amg-psp-wallpaper-2/

    Would you be able to log in and see ?

  6. whiteknightuk
    Member
    Posted 4 years ago #

    If your logged into Facebook the style breaks, just tried whilst logged out. Also I was using Chrome.

  7. Samuel Wood (Otto)
    Tech Ninja
    Posted 4 years ago #

    I don't know what to tell you. The code I posted is correct.

    Your styling might break anyway with the Facebook avatars, because they use a different set of tags. That's why they have special classes like fbavatar and such. You'll have to add some extra styling rules to make them look the same as the other avatars.

  8. syssu
    Member
    Posted 4 years ago #

    Hello, I'm using your SFC and STC plugins and I am having the problem where the avatars do not show up and this is what was the suggested problem. I'm using the Thesis theme over at http://www.diythemes.com, any suggestions?

  9. Samuel Wood (Otto)
    Tech Ninja
    Posted 4 years ago #

    Lots of people have problems with Thesis. From what I've been hearing via email, it seems to me to just be a poorly written theme that doesn't use standard methods for virtually anything. At the moment, I'd have to recommend not using it at all.

    I'd love to tell you how to fix it, but they don't have a version available for download that I can see. Sorry.

  10. mrjcleaver
    Member
    Posted 4 years ago #

    Does that mean that the default buddypress theme (v1.2.3) is wrong?

    /var/www/wp-content/themes/twentyten 09:28:01 504$ grep get_avatar *
    author.php: <?php echo get_avatar( get_the_author_meta( 'user_email' ), apply_filters( 'twentyten_author_bio_avatar_size', 60 ) ); ?>
    functions.php: <?php echo get_avatar( $comment, 40 ); ?>
    single.php: <?php echo get_avatar( get_the_author_meta( 'user_email' ), apply_filters( 'twentyten_author_bio_avatar_size', 60 ) ); ?>

    Thanks, M.

  11. Samuel Wood (Otto)
    Tech Ninja
    Posted 4 years ago #

    No, this only applies to showing avatars for comments.

  12. saiftheboss7
    Member
    Posted 4 years ago #

    My Thesis Theme Call Avatar Like This

    function avatar() {
    $avatar = get_avatar(get_comment_author_email(), $this->meta['avatar']['options']['size'], '');
    $author_url = get_comment_author_url();
    $avatar_output = (empty($author_url) || $author_url == 'http://') ? $avatar : "$avatar";
    return '<span class="avatar">' . apply_filters('thesis_avatar', $avatar_output) . '</span>'; #filter
    }

    What WIll I Change?

  13. Samuel Wood (Otto)
    Tech Ninja
    Posted 4 years ago #

    Corrected way to do it:

    function avatar() {
    global $comment;
    $avatar = get_avatar($comment, $this->meta['avatar']['options']['size'], '');
    $author_url = get_comment_author_url();
    $avatar_output = (empty($author_url) || $author_url == 'http://') ? $avatar : "$avatar";
    return '<span class="avatar">' . apply_filters('thesis_avatar', $avatar_output) . '</span>'; #filter
    }
  14. DevonHarper
    Member
    Posted 4 years ago #

    Otto42,
    Thank you for your suggestion on properly passing the entire comment object to the get_avatar function! With your help I was able to modify the thematic_commenter_link function for my Thematic child theme to work properly with your plugin. My modified version of the Thematic function is:


    // Produces an avatar image with the hCard-compliant photo class
    function childtheme_commenter_link() {
    global $comment;
    $commenter = get_comment_author_link();
    if ( ereg( '<a[^>]* class=[^>]+>', $commenter ) ) {
    $commenter = ereg_replace( '(<a[^>]* class=[\'"]?)', '\\1url ' , $commenter );
    } else {
    $commenter = ereg_replace( '(<a )/', '\\1class="url "' , $commenter );
    }
    $avatar_size = apply_filters( 'avatar_size', '80' ); // Available filter: avatar_size
    $avatar = str_replace( "class='avatar", "class='photo avatar", get_avatar( $comment, $avatar_size ) );
    echo $avatar . ' <span class="fn n">' . $commenter . '</span>';
    } // end childtheme_commenter_link

    I posted a full deescription of how I setup the action overrides on the ThemeShaper Forums.

  15. ranji77
    Member
    Posted 4 years ago #

    This is excellent, thank you Otto!

    $comment worked much better for me than the default of $id_or_email

  16. Kasper.
    Member
    Posted 3 years ago #

    @DevonHarper
    Thanks, works great for me. BTW, I'm using Autofokus+ Pro as a childtheme.

  17. BusterSenshi
    Member
    Posted 3 years ago #

    Hey Otto,

    i have tried fixing my themes code:

    The code on the comments.php is:

    if (function_exists('get_avatar')) {
           echo get_avatar(get_comment_author_email(), '40');
       } else {
    	  $gravatarUrl = "http://www.gravatar.com/avatar.php?gravatar_id=" . md5(get_comment_author_email()) . "&size=40";
          echo "<img src='$gravatarUrl'/>";
       }

    Any ideas?

  18. Samuel Wood (Otto)
    Tech Ninja
    Posted 3 years ago #

    Change this:
    echo get_avatar(get_comment_author_email(), '40');

    To this:

    global $comment;
    echo get_avatar($comment, '40');
  19. BusterSenshi
    Member
    Posted 3 years ago #

    Thank you so much Otto

  20. RaidX
    Member
    Posted 3 years ago #

    With combination I also use alt tags and set width and hight for SEO Reasons.

    $Somerandgrav = md5(get_comment_author_email() . rand(1,1000));
    $gravatarUrl = "http://www.gravatar.com/avatar.php?gravatar_id=" . md5(get_comment_author_email()) . "&size=40";
    echo "<img src='$gravatarUrl' height='50' width='50' alt='$Somerandgrav' />";

    The rand is created in case avatar is displayed more than once to prevent conflicting Alt tags.

  21. Samuel Wood (Otto)
    Tech Ninja
    Posted 3 years ago #

    alt tags don't have to be unique.

    And your code won't work with any plugin that modifies the avatars. Use the standard get_avatar code instead.

  22. jhayrocas
    Member
    Posted 3 years ago #

    I tried this code on Thesis:

    global $comment;
    $avatar = get_avatar($comment, $this->meta['avatar']['options']['size'], '');
    $author_url = get_comment_author_url();
    $avatar_output = (empty($author_url) || $author_url == 'http://') ? $avatar : "$avatar";
    return '<span class="avatar">' . apply_filters('thesis_avatar', $avatar_output) . '</span>'; #filter
    }

    But it returned with the error message:

    Fatal error: Using $this when not in object context in /home/jrocas/public_html/wp-content/themes/thesis_18/custom/custom_functions.php on line 344

    That line of code in question is:

    $author_url = get_comment_author_url();

    Any ideas?

Topic Closed

This topic has been closed to new replies.

About this Topic