Wordpress缓存Gravatar 头像(方法二)
Wordpress缓存 Gravatar 头像(方法二)
Function代码
//缓存Gravatar头像二,by minirizhi.com
function minirizhi_cache_avatar($avatar, $id_or_email, $size, $default, $alt)
{
$avatar = str_replace(array("cn.gravatar.com", "secure.gravatar.com", "0.gravatar.com", "1.gravatar.com", "2.gravatar.com"), "www.gravatar.com", $avatar);
$tmp = strpos($avatar, 'http');
$url = get_avatar_url( $id_or_email, $size ) ;
$url = str_replace(array("cn.gravatar.com", "secure.gravatar.com", "0.gravatar.com", "1.gravatar.com", "2.gravatar.com"), "www.gravatar.com", $url);
$avatar2x = get_avatar_url( $id_or_email, ( $size * 2 ) ) ;
$avatar2x = str_replace(array("cn.gravatar.com", "secure.gravatar.com", "0.gravatar.com", "1.gravatar.com", "2.gravatar.com"), "www.gravatar.com", $avatar2x);
$g = substr($avatar, $tmp, strpos($avatar, "'", $tmp) - $tmp);
$tmp = strpos($g, 'avatar/') + 7;
$f = substr($g, $tmp, strpos($g, "?", $tmp) - $tmp);
$w = home_url();
$e = ABSPATH .'avatar/'. $size . '*'. $f .'.jpg';
$e2x = ABSPATH .'avatar/'. ( $size * 2 ) . '*'. $f .'.jpg';
$t = 1209600;
if ( (!is_file($e) || (time() - filemtime($e)) > $t) && (!is_file($e2x) || (time() - filemtime($e2x)) > $t ) ) {
copy(htmlspecialchars_decode($g), $e);
copy(htmlspecialchars_decode($avatar2x), $e2x);
} else { $avatar = $w.'/avatar/'. $size . '*'.$f.'.jpg';
$avatar2x = $w.'/avatar/'. ( $size * 2) . '*'.$f.'.jpg';
if (filesize($e) < 1000) copy($w.'/avatar/default.jpg', $e);
if (filesize($e2x) < 1000) copy($w.'/avatar/default.jpg', $e2x);
$avatar = "<img alt='{$alt}' src='{$avatar}' srcset='{$avatar2x}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
}
return $avatar;
}
add_filter('get_avatar', 'minirizhi_cache_avatar',1,5);
文章目录
- 上一篇: Wordpress缓存Gravatar 头像(方法一)
- 下一篇: 写给自己