Title: using gravatar code elsewhere
Last modified: August 18, 2016

---

# using gravatar code elsewhere

 *  [lokjah](https://wordpress.org/support/users/lokjah/)
 * (@lokjah)
 * [21 years, 2 months ago](https://wordpress.org/support/topic/using-gravatar-code-elsewhere/)
 * Im trying to incorporate the gravatar code in other areas of my site and seem
   to be havin a bit of trouble getting it to show up…. at the moment i’m trying
   to add it to the get-commenters code by coffee2code and after alot of attempts
   the closest I can come is getting no php errors but its printing out the last
   bits of the img src
    `bruce" alt="" /> (2) `
 * when using the gravatar code as such :
 * `foreach ($commenters as $commenter) {
    echo $before; if (!empty($commenter->
   comment_author_url)) echo '<a title="Visit ' . $commenter->comment_author . ''
   s site" href="' . $commenter->comment_author_url . '"'; if ($open_in_new_window)
   echo ' target="_blank"'; if (!empty($commenter->comment_author_url)) echo '>';
   echo $commenter->comment_author; if (!empty($commenter->comment_author_url)) 
   echo '</a>'; if (!empty($commenter->comment_author_url)) echo '<img src= "<?php
   gravatar("G", 50, "http://mysite.com/graphics/grav.gif"); ?>" alt="" />'; if ('
   top' == $type) echo ' (' . $commenter->total_comments . ')'; if (!$omit_last_after)
   echo $after; echo "n"; }
 * anyone spot what Im doin wrong?

Viewing 15 replies - 1 through 15 (of 23 total)

1 [2](https://wordpress.org/support/topic/using-gravatar-code-elsewhere/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/using-gravatar-code-elsewhere/page/2/?output_format=md)

 *  [Nick Momrik](https://wordpress.org/support/users/mtdewvirus/)
 * (@mtdewvirus)
 * [21 years, 2 months ago](https://wordpress.org/support/topic/using-gravatar-code-elsewhere/#post-156385)
 * The gravatar plugin code already does an echo, so you can’t echo the echo.
 * Try changing
    ` if (!empty($commenter->comment_author_url)) echo "<img src= "
   <?php gravatar("G", 50, "http://mysite.com/graphics/grav.gif"); ?>" alt="" />";`
 * to this
    ` if (!empty($commenter->comment_author_url)) { echo "<img src= ""; 
   gravatar("G", 50, "http://mysite.com/graphics/grav.gif"); echo "" alt="" />";}
 *  Thread Starter [lokjah](https://wordpress.org/support/users/lokjah/)
 * (@lokjah)
 * [21 years, 2 months ago](https://wordpress.org/support/topic/using-gravatar-code-elsewhere/#post-156393)
 * thanks a ton man…. never would have figured that out…
 * only thing now is im getting erratic results on what gravatar is showing up….
   im using this code in a sidebar and when the main index page is viewed I get 
   the generic gravatar (used for commenters with no gravatar) for all commenters.
   then when viewing an actual post I can see my custom gravatar shows up, but in
   some instances it is also being used for other commenters, and in other cases
   other commenters have no image at all…..
 * could it be how the gravatar code is written in your example or maybe something
   else?
 * thanks again woot!
 *  [Nick Momrik](https://wordpress.org/support/users/mtdewvirus/)
 * (@mtdewvirus)
 * [21 years, 2 months ago](https://wordpress.org/support/topic/using-gravatar-code-elsewhere/#post-156395)
 * I had left off the closing backtick for the code block. Try the code again.
 *  Thread Starter [lokjah](https://wordpress.org/support/users/lokjah/)
 * (@lokjah)
 * [21 years, 2 months ago](https://wordpress.org/support/topic/using-gravatar-code-elsewhere/#post-156398)
 * hmm I get a parse error with this code
 * `if (!empty($commenter->comment_author_url)) {
    echo "<img src= ""; gravatar("
   G", 50, "http://www.mysite.com/graphics/grav.gif"); echo "" alt="" />"; }
 * “Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ‘,’
   or ‘;’ in /home/aenonf2/public_html/afd/blog/wp-content/plugins/get-commenters.
   php on line 106”
 *  Thread Starter [lokjah](https://wordpress.org/support/users/lokjah/)
 * (@lokjah)
 * [21 years, 2 months ago](https://wordpress.org/support/topic/using-gravatar-code-elsewhere/#post-156415)
 * using your first code:
 * `if (!empty($commenter->comment_author_url)) {
    echo '<img src= "'; gravatar("
   G", 50, "http://www.mysite.com/graphics/grav.gif"); echo '" alt="" />'; }
 * works but the gravatar results are sporadic as noted above
 *  Thread Starter [lokjah](https://wordpress.org/support/users/lokjah/)
 * (@lokjah)
 * [21 years, 2 months ago](https://wordpress.org/support/topic/using-gravatar-code-elsewhere/#post-156586)
 * using single quotes works but double does not, while the single ones work could
   it be borking the way the gravatar gets the author info?
 *  Thread Starter [lokjah](https://wordpress.org/support/users/lokjah/)
 * (@lokjah)
 * [21 years, 2 months ago](https://wordpress.org/support/topic/using-gravatar-code-elsewhere/#post-156649)
 * any takers? 🙂
 *  [Nick Momrik](https://wordpress.org/support/users/mtdewvirus/)
 * (@mtdewvirus)
 * [21 years, 2 months ago](https://wordpress.org/support/topic/using-gravatar-code-elsewhere/#post-156664)
 * ` if (!empty($commenter->comment_author_url)) {
    echo '<img src= "'; gravatar("
   G", 50, "http://mysite.com/graphics/grav.gif"); echo '" alt="" />'; }
 * I think bbPress ate the code. This one should work.
 *  Thread Starter [lokjah](https://wordpress.org/support/users/lokjah/)
 * (@lokjah)
 * [21 years, 2 months ago](https://wordpress.org/support/topic/using-gravatar-code-elsewhere/#post-156666)
 * well thanks alot for your help mtdv, that code indeed works with no php errors,
   just still get unpredictable gravatar results, eg, on the index page it shows
   the default gravatar for everyone, on a post page it shows my custom gravatar
   for me as well as other posters, and on other posts it will show my custom gravatar
   and no gravatar for other commenters.
 * is there a way maybe I could write the gravatar code by itself apart from the
   get commenters plugin?
 *  [Nick Momrik](https://wordpress.org/support/users/mtdewvirus/)
 * (@mtdewvirus)
 * [21 years, 2 months ago](https://wordpress.org/support/topic/using-gravatar-code-elsewhere/#post-156667)
 * I don’t know how I missed this before, but instead of
    ` if (!empty($commenter-
   >comment_author_url)) {` it should be ` if (!empty($comment->comment_author_email)){`
   since gravatars are linked to email address, not URL. Try changing that line 
   and see what happens.
 *  [Nick Momrik](https://wordpress.org/support/users/mtdewvirus/)
 * (@mtdewvirus)
 * [21 years, 2 months ago](https://wordpress.org/support/topic/using-gravatar-code-elsewhere/#post-156668)
 * Whoops….make that $commenter, not $comment
 *  Thread Starter [lokjah](https://wordpress.org/support/users/lokjah/)
 * (@lokjah)
 * [21 years, 2 months ago](https://wordpress.org/support/topic/using-gravatar-code-elsewhere/#post-156671)
 * huh very strange man.. u know that didnt make a lick a difference…..
 * still showin up unpredictably….
 *  [Nick Momrik](https://wordpress.org/support/users/mtdewvirus/)
 * (@mtdewvirus)
 * [21 years, 2 months ago](https://wordpress.org/support/topic/using-gravatar-code-elsewhere/#post-156673)
 * Is the plugin pulling comment_author_email in the query to the database? Maybe
   you can paste the code up here…
    [http://www.pastebin.com/](http://www.pastebin.com/)
   and post back here with a link to it.
 *  Thread Starter [lokjah](https://wordpress.org/support/users/lokjah/)
 * (@lokjah)
 * [21 years, 2 months ago](https://wordpress.org/support/topic/using-gravatar-code-elsewhere/#post-156674)
 * hope i did this right man.. heres the link to the plugin code
 * [http://www.pastebin.com/247706](http://www.pastebin.com/247706)
 *  [Nick Momrik](https://wordpress.org/support/users/mtdewvirus/)
 * (@mtdewvirus)
 * [21 years, 2 months ago](https://wordpress.org/support/topic/using-gravatar-code-elsewhere/#post-156676)
 * I don’t know what to tell you. I can’t see anything jumping out at me. Only thing
   I can think of is to remove the if block around that gravatar code, because now
   that I think of it, the if isn’t needed. Gravatar should work with or without
   an email address passed to it. If it doens’t get one, it simply displays your
   default image.
 * By the way…you are replacing “[http://mysite.com/graphics/grav.gif&#8221](http://mysite.com/graphics/grav.gif&#8221);
   with the location of your own default image right?

Viewing 15 replies - 1 through 15 (of 23 total)

1 [2](https://wordpress.org/support/topic/using-gravatar-code-elsewhere/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/using-gravatar-code-elsewhere/page/2/?output_format=md)

The topic ‘using gravatar code elsewhere’ is closed to new replies.

 * 23 replies
 * 3 participants
 * Last reply from: [7milesdown](https://wordpress.org/support/users/7milesdown/)
 * Last activity: [21 years, 2 months ago](https://wordpress.org/support/topic/using-gravatar-code-elsewhere/page/2/#post-156730)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
