WordPressÖÐGravatarÍ·Ïñ»º´æµ½±¾µØ¼°Ïà¹ØÓÅ»¯µÄ¼¼ÇÉ

5年以前  |  阅读数:969 次  |  编程语言:PHP 

½«GravatarÈ«ÇoͨÓÃÍÏñ»º´aeµÄÄ¿µÄÔÚÓÚ¼Ó¿iÍøÕ¾µÄ´o¿ªËٶȣ¬ÒoΪGravatar¹ÙÍøµÄþÎñÆ÷ÔÚ¹uÍa£¬¼ÓÉÏΰ´oµÄGFW£¬¹uÄÚ´o¿ªËٶȾ­³£ºÜÂý¡£½¨À´×Ôwillin,²»¹ýòËÆËuµÄÍøÕ¾ÒѾ­´o²»¿ªÁË- -
*
½«GravatarÈ«ÇoͨÓÃÍ
Ïñ»º´aeµ½±¾µØ
*
»º´ae
½*¨ÈçÏ£º

1.½¨Á¢»º´aeĿ¼
ÔÚWordPress¸uĿ¼½¨Á¢Ò»¸oÃuΪ avatarµÄÎļþ¼Ð£¬ÉeÖøÃÎļþ¼ÐµÄȨÏÞΪ 0755 (Èç¹u 0755 ²»ÐУ¬¾ÍÊÔһϠ0777)¡£

2.ÉeÖÃĬÈÏÍÏñ
×¼±¸Ò»ÕÅ´oСÊʺÏ(32
32¼´¿É)µÄĬÈÏÍÏñ,ÃuÃuΪ"default.jpg" £¬ÅÔÚ avatar Îļþ¼ÐÀiÃae¡£

3.Ìi¼Ó»º´ae´uÂe
½«ÏÂÃaeµÄ´uÂe¸´ÖƵ½Ö÷ÌaµÄ functions.php ÎļþÖм´¿É


    function my_avatar($avatar) {
    $tmp = strpos($avatar, 'http');
    $g = substr($avatar, $tmp, strpos($avatar, "'", $tmp) - $tmp);
    $tmp = strpos($g, 'avatar/') + 7;
    $f = substr($g, $tmp, strpos($g, "?", $tmp) - $tmp);
    $w = get_bloginfo('wpurl');
    $e = ABSPATH .'avatar/'. $f .'.jpg';
    $t = 1209600; //ÔO¶¨14Ìi, †Îλ:Ãe
    if ( !is_file($e) || (time() - filemtime($e)) > $t ) { //®"i^Ïñ²»´aeÔÚ»oÎļþ³¬ß^14Ìi²Å¸uÐÂ
    copy(htmlspecialchars_decode($g), $e);
    } else $avatar = strtr($avatar, array($g => $w.'/avatar/'.$f.'.jpg'));
    if (filesize($e) < 500) copy($w.'/avatar/default.jpg', $e);
    return $avatar;
    }
    add_filter('get_avatar', 'my_avatar');

ͨ¹ýEmailÅжÏÓû§ÊÇñÓÐGravatarÍÏñ
ºÜ¶aʱºo£¬ÎÒÃÇ»¹ÐeÒªÖªµÀÓû§ÓÐûÓÐÉeÖÃGravatar£¬±ÈÈçÈç¹uÓû§Ã»ÓÐÉeÖÃGravatarÍÏñÔoÖ±½ÓÏÔʾ±¾µØµÄĬÈÏÍÏñ£¬»oÕßÌaÐÑûÓÐÉeÖÃGravatarµÄÓû§ÉeÖÃGravatarÍÏñ£¬¶ø²»´oÈÅÒѾ­ÉeÖùýÍÏñµÄÓû§¡£½ñÌiÖÏiµÄ´uÂe¿ÉÒÔͨ¹ýEmailµØÖÀ´ÑeÖ¤Óû§ÊÇñÓÐGravatarÍÏñ£º


    function validate_gravatar($email) {
     $hash = md5(strtolower(trim($email)));
     $uri = 'http://www.gravatar.com/avatar/' . $hash . '?d=404';
     $headers = @get_headers($uri);
     if (!preg_match("|200|", $headers[0])) {
     $has_valid_avatar = FALSE;
     } else {
     $has_valid_avatar = TRUE;
     }
     return $has_valid_avatar;
    }

*GravatarÈ«ÇoͨÓÃÍÏñ»º´aeÓÅ»¯**

ÓÅ»¯ºoµÄ´uÂe¿ÉÒÔ±ÜÃaÓû§Ã»ÓÐÉeÖÃGravatarÍÏñʱ£¬Ä¬ÈÏÍÏñÖظ´»º´ae³É¶a¸oÎļþÔi³É×ÊÔ´ÀË*ѺÍÖظ´Á¬½ÓµÄÎÊÌa¡£ÓÅ»¯ºoµÄ´uÂeÈçÏÂ


    function my_avatar( $email, $size = '32', $default = '', $alt = '') {
     $f = md5( strtolower( $email ) );
     // ÒÔÏ´uÂe½«Í*Ïñ»º´aeµ½wp-contentĿ¼ÏÂ
     $a = get_bloginfo('template_url') . '/avatar/'. $f . $size . '.png';
     $e = get_template_directory() . '/avatar/' . $f . $size . '.png';
     $d = get_template_directory() . '/avatar/' . $f . '-d.png';
     // Èç¹uÒª½«Í*Ïñ»º´aeµ½µ±Ç°Ö÷ÌaĿ¼Ï£¬Çe½«3-5ÐдuÂe¸Ä³É£º
     // $a = get_bloginfo('template_url') . '/avatar/'. $f . $size . '.png';
     // $e = get_template_directory() . '/avatar/' . $f . $size . '.png';
     // $d = get_template_directory() . '/avatar/' . $f . '-d.png';

    if($default=='')
      $default = get_bloginfo('wpurl').'avatar/default.jpg';

     $t = 2592000; // »º´aeÓÐЧÆÚ30Ìi, ÕaÀiµ¥Î»:Ãe
     if ( !is_file($e) || (time() - filemtime($e)) > $t ) {
      if ( !is_file($d) || (time() - filemtime($d)) > $t ) {
       // ÑeÖ¤ÊÇ*ñÓÐÍ*Ïñ
       $uri = 'http://www.gravatar.com/avatar/' . $f . '?d=404';
       $headers = @get_headers($uri);
       if (!preg_match("|200|", $headers[0])) {
        // ûÓÐÍ*Ïñ£¬Ôoн¨Ò»¸o¿Õ°×Îļþ×÷Ϊ±e¼Ç
        $handle = fopen($d, 'w');
        fclose($handle);
        $a = $default;
       }
       else {
        // ÓÐÍ*ÏñÇÒ²»´aeÔÚÔo¸uÐÂ
        $r = get_option('avatar_rating');
        $g = 'http://www.gravatar.com/avatar/'. $f. '?s='. $size. '&r;=' . $r;
        copy($g, $e);
       }
      }
      else {
       $a = $default;
      }
     }

     $avatar = "<img alt='{$alt}' src='{$a}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
     return apply_filters('my_avatar', $avatar, $email, $size, $default, $alt);
    }

ʹÓÃÕa¸o½¨ÐeÒªÔÚÖ÷ÌaËuÓÐÎļþÖУ¬½« get_avatar º¯ÊýÌae»»Îª my_avatar ¡£
¶øÇÒÈç¹uÊÇ


    get_avatar( $comment,

¸ñʽµÄ»°£¬»¹ÐeÒª¸Ä³É


    my_avatar( $comment->comment_author_email

ÒoΪmy_avatarº¯ÊýÖ»ÄÜͨ¹ýEmailÀ´µ÷È¡Óû§ÍÏñ£¬ËuÒÔÒÔÉÏÇe¿o£¬ÐeÒª½«µÚÒ»¸o²ÎÊý¸Ä³ÉemailµØÖ¡£

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例