½«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СÊʺÏ(3232¼´¿É)µÄĬÈÏÍÏñ,Ã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µØÖ¡£