php 在WordPress评论中显示Google个人资料图片,如果作者是gmail.com,则为gravatar

1qczuiv0  于 11个月前  发布在  PHP
关注(0)|答案(1)|浏览(118)

我想显示评论作者谷歌个人资料图片,如果他输入了一个gmail.com电子邮件地址,否则我会显示在评论的gravatar。
由于我在编码方面的限制,我设法将示例代码进一步构造:

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 - [email protected]
        //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(); ?>


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

yyhrrdl8

yyhrrdl81#

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

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 - [email protected]
  // 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
 }

字符串

相关问题