WordPress gravatar / avatar bug: comment email instead of admin email

The following can happen:

  1. You make a site for a customer
  2. You insert some comments that your customer provides.
  3. Gravatar uses the admin email instead of the comment author email.

How do you get a gravatar image for the comment email instead of the author email?

You need to write a filter function. the get_avatar filter passes the img code as first param, and the id of the author(you) or an emailaddress as the second.

So we check if the second param is an emailaddress, then the code can pass. Otherwise, we need to check the debug backtrace. One of the calling functions in the trace might have an emailaddress set. When we find that (we use a simple loop), we can generate a md5 hash and replace the hash in the first param so the right emailaddress is passed to gravatar(and not your own).

Use the following filter:

function my_filter_get_avatar($avatar, $id_or_email) {
    if (filter_var($id_or_email, FILTER_VALIDATE_EMAIL))
        return $avatar;

    // Force avatar based on comment email instead of author id(if present).
    $hash = null;
    $backtrace = debug_backtrace();
    foreach ($backtrace as $trace) {
        if (isset($trace['args'][0]->comment_author_email)) {
            $hash = md5(trim(strtolower($trace['args'][0]->comment_author_email)));

    if ($hash) {
        $avatar = preg_replace('@[a-z0-9]{32}@i', $hash, $avatar);

    return $avatar;
add_filter('get_avatar', 'my_filter_get_avatar', 10, 2);

Geef een reactie

Het e-mailadres wordt niet gepubliceerd. Verplichte velden zijn gemarkeerd met *