0

I'm trying to display user gravatars on this page http://gepc.themelot.com/survey-comparison/433747624/293281083/ but the page keeps showing the default 'mystery man' avatar. I inspected the page element and it returns the same URL with the same hash value for the gravatar link.

Here is my code:

<?php if (!empty($matched_answers)) { ?>
    <h1><?php $user = get_user_by( 'user_login', $matched_answers[0]['user'] ); echo get_avatar( $user->ID , 256 ).' '.$matched_answers[0]['user'].' - '.round($matched_answers[0]['percentage'], 2).' &#37;'; ?></h1>
    <table border="0" width="100%">
        <thead>
        <th>Name</th>
        <th>Percentage</th>
        </thead>
        <tbody>
            <?php
            foreach ($matched_answers as $value) {
                $user = get_user_by( 'user_login', $value['user'] );
                $user_email = $user->user_email;
                $url = 'http://gravatar.com/avatar/' . md5( $user_email );
                $url = add_query_arg( array(
                's' => 48,
                'd' => 'mm',
                ), $url );
                $gravatar_url = esc_url_raw( $url );
                echo '<tr>';
                echo '<td>'.'<img src='.$gravatar_url.' />'.'<td>';
                echo '<td>'.$value['user'] . '</td>';
                echo '<td class="votepercent">' . round($value['percentage'], 2) . ' &#37;</td>';
                echo '</tr>';
            }
            ?>
        </tbody>
    </table>  
<?php }
?>

How do I return the user's actual gravatar image? What is wrong with my code?

Thanks in advance.

0

Not all your images on that page have issues. For example: http://gravatar.com/avatar/619d8e38359241a952a1f9b67f1f36cd?s=60&d=mm

The rest of the images aren't displaying because the emails you've encoded are not recognized with gravatar. The default image shows because you are specifying to use mm when the image can't be found for your hash. They have several tips for normalizing the email like trim and strtolower to get a better hash hit.

Creating a hash:

  1. Trim leading and trailing whitespace from an email address
  2. Force all characters to lower-case
  3. md5 hash the final string

echo md5( strtolower( trim( "MyEmailAddress@example.com " ) ) );

  $user = get_user_by( 'user_login', $value['user'] );
  $user_email = $user->user_email;

  $url = 'http://gravatar.com/avatar/' . md5(strtolower(trim($user_email)));
  $url = add_query_arg( array(
    's' => 80,
    'd' => 'mm',
  ), $url );

  $gravatar_url = esc_url_raw( $url );

Gravatar API - Default Image

Change the default image from mm to https%3A%2F%2Fplacekitten.com%2F256%2F256 by specifying the d parameter.

http://gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=250&d=https%3A%2F%2Fplacekitten.com%2F256%2F256

404: do not load any image if none is associated with the email hash, instead return an HTTP 404 (File Not Found) response

  • mm: (mystery-man) a simple, cartoon-style silhouetted outline of a person (does not vary by email hash)
  • identicon: a geometric pattern based on an email hash
  • monsterid: a generated 'monster' with different colors, faces, etc
  • wavatar: generated faces with differing features and backgrounds
  • retro: awesome generated, 8-bit arcade-style pixelated faces
  • blank: a transparent PNG image (border added to HTML below for demonstration purposes)

Just remember, not every email has a gravatar which is why the default image exists.

  • I edited the code with yours but I still get the same image. The same URL is repeated. Please see the implementation in the question. – Sosthenes Kwame Boame Aug 14 '16 at 8:53
  • Updated with more info from the API – jgraup Aug 16 '16 at 3:58
0

At first glance it looks more or less OK. Have you considered that these users simply don't have a Gravatar associated with their email?

Generally speaking, one should use built-in WordPress functions whenever possible to reduce risk of errors and making the code more future proof. For Gravatar, there is get_avatar(). For your use case, it would look like this:

$user_id = 0;
$user = get_user_by( 'user_login', $value['user'] );
if ( $user ) {
  $user_id = $user->ID;
}
echo '<tr>';
echo '<td>' . get_avatar( $user_id, 48, 'mm' ) .'<td>';

As you can see, you can pass the user ID (or an email address) to the function, specify the size and the default avatar. The function returns the complete <img> tag with the avatar, making the code much more readable.

If there's still no individual Gravatar being displayed, the users really do not have a Gravatar. Simple as that.

0

I found a solution to my specific case. I ended up using get_user_by('login', $value) Here is my code:

    <?php
    foreach ($matched_answers as $value) {
    $user           = get_user_by('login', $value['user']);
    $gravatar_url = get_avatar( $user->user_email, 60);        
    echo '<tr style="margin-bottom:10px;">';
    echo '<td width="33.33333%">' . $gravatar_url . '</td>';
    echo '<td width="33.33333%" class="votepercent">' . round($value['percentage'], 2) . ' &#37; of issues</td>';
    echo '<td width="33.33333%" class="tbtn"><a class="btn btn-primary">View Responses</a></td>';
    echo '</tr>';
    }?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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