在Wordpress评论中显示Google个人资料图像.如果作者是 gmail.com 其他头像


Show Google profile image in Wordpress comments. if author is gmail.com else gravatar

我想显示评论作者谷歌个人资料图片,如果他输入了 gmail.com 电子邮件地址,否则我将在评论中显示头像。

由于我在编码方面的限制,我设法将示例代码进一步构建:

function comment_image() {
$email = get_avatar(get_comment_author_email());
$domains = array('gmail.com', 'google.com');
$pattern = "/^[a-z0-9._%+-]+@[a-z0-9.-]*(" . implode('|', $domains) . ")$/i";
if (preg_match($pattern, $email)) {
    function email_to_userid() {
        // get user id of the email address - xyz@gmail.com
        //request google profile image url eg: https://www.googleapis.com/plus/v1/people/123456789?fields=image&key={API_KEY}
        // above will retun URL:  "url": "https://lh3.googleusercontent.com/-abcdef/bbbbbas/photo.jpg?sz=50"
        // return the image URL
    }
}
   } elseif; {
   echo get_avatar($comment, 60);
}

我将在我的评论模板中调用上述函数来显示图像:

<?php echo comments_image(); ?>

提前感谢这个伟大的社区。

如果你的问题纯粹是句法上的,这应该会有所帮助:

function comments_image() {
  $email = get_avatar(get_comment_author_email());
  $domains = array('gmail.com', 'google.com');
  $pattern = "/^[a-z0-9._%+-]+@[a-z0-9.-]*(" . implode('|', $domains) . ")$/i";
  if (preg_match($pattern, $email)) {
    email_to_userid($email);
  } elseif {
    echo get_avatar($comment, 60);
  }
}
function email_to_userid($email) {
  // get user id of the email address - xyz@gmail.com
  // request google profile image url eg: https://www.googleapis.com/plus/v1/people/123456789?fields=image&key={API_KEY}
  // above will retun URL:  "url": "https://lh3.googleusercontent.com/-abcdef/bbbbbas/photo.jpg?sz=50"
  // return the image URL
 }