思章老师
思章老师

WordPress头像缓存:WP Gravatar Mini Cache V1.0.4

近来博客联盟网站因备案临时关闭,我时间就多了,就利用这几天折腾一下WordPress,今天的新主题 LSZ Thinky Pro V3.1 准备启用头像缓存,以提升速度,向大家推荐WordPress头像缓存:WP Gravatar Mini Cache V1.0.4。迷你 Gravatar 头像缓存插件,可以将 Gravatar 头像缓存至本地,以加快加载速度,使其不受 Gravatar 服务器连接速度。

也可以直接把以下代码复制到functions.php的 之间。

if ( !function_exists( 'get_avatar' ) ) :
function get_avatar( $id_or_email, $size = '96', $default = '', $alt = false ) {
  if ( ! get_option('show_avatars') )
    return false;
 
  if ( false === $alt)
    $safe_alt = '';
  else
    $safe_alt = esc_attr( $alt );
 
  if ( !is_numeric($size) )
    $size = '96';
 
  $email = '';
  if ( is_numeric($id_or_email) ) {
    $id = (int) $id_or_email;
    $user = get_userdata($id);
    if ( $user )
      $email = $user->user_email;
  } elseif ( is_object($id_or_email) ) {
    // No avatar for pingbacks or trackbacks
    $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
    if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) )
      return false;
 
    if ( !empty($id_or_email->user_id) ) {
      $id = (int) $id_or_email->user_id;
      $user = get_userdata($id);
      if ( $user)
        $email = $user->user_email;
    } elseif ( !empty($id_or_email->comment_author_email) ) {
      $email = $id_or_email->comment_author_email;
    }
  } else {
    $email = $id_or_email;
  }
 
  if ( empty($default) ) {
    $avatar_default = get_option('avatar_default');
    if ( empty($avatar_default) )
      $default = 'mystery';
    else
      $default = $avatar_default;
  }
 
  if ( !empty($email) )
    $email_hash = md5( strtolower( $email ) );
 
  if ( is_ssl() ) {
    $host = 'https://secure.gravatar.com';
  } else {
    if ( !empty($email) )
      $host = sprintf( "http://%d.gravatar.com", ( hexdec( $email_hash{0} ) % 2 ) );
    else
      $host = 'http://0.gravatar.com';
  }
 
  if ( 'mystery' == $default )
    $default = "$host/avatar/ad516503a11cd5ca435acc9bb6523536?s={$size}"; // ad516503a11cd5ca435acc9bb6523536 == md5('unknown@gravatar.com')
  elseif ( 'blank' == $default )
    $default = includes_url('images/blank.gif');
  elseif ( !empty($email) && 'gravatar_default' == $default )
    $default = '';
  elseif ( 'gravatar_default' == $default )
    $default = "$host/avatar/s={$size}";
  elseif ( empty($email) )
    $default = "$host/avatar/?d=$default&s={$size}";
  elseif ( strpos($default, 'http://') === 0 )
    $default = add_query_arg( 's', $size, $default );
 
  if ( !empty($email) ) {
    $out = "$host/avatar/";
    $out .= $email_hash;
    $out .= '?s='.$size;
    $out .= '&d=' . urlencode( $default );
 
    $rating = get_option('avatar_rating');
    if ( !empty( $rating ) )
      $out .= "&r={$rating}";
 
//--- WP Gravatar Mini Cache v1.0.3 --- Start ------------------------------
 
  $ev_time = 1209600;
  if (!is_dir(ABSPATH.'ev-iava/')) { mkdir(ABSPATH.'ev-iava/'); }
  $ev_abs = ABSPATH.'ev-iava/'.$email_hash.'_'.$size.'.jpg';
  $ev_url = get_bloginfo('wpurl').'/ev-iava/'.$email_hash.'_'.$size.'.jpg';
  if (!is_file($ev_abs)||(time()-filemtime($ev_abs))>$ev_time){
    copy($out,$ev_abs);
  }
  $out = $ev_url;
 
//--- WP Gravatar Mini Cache v1.0.3 --- End --------------------------------
 
    $avatar = "
{$safe_alt}
"; } else { //--- WP Gravatar Mini Cache v1.0.3 --- Start ------------------------------ $ev_abs = ABSPATH.'ev-iava/'.$size.'.jpg'; $ev_url = get_bloginfo('wpurl').'/ev-iava/'.$size.'.jpg'; if (!is_file($ev_abs)||(time()-filemtime($ev_abs))>$ev_time){ copy($default,$ev_abs); } $default = $ev_url; //--- WP Gravatar Mini Cache v1.0.3 --- End -------------------------------- $avatar = "
{$safe_alt}
"; } return apply_filters('get_avatar', $avatar, $id_or_email, $size, $default, $alt); } endif;

跟踪WP官方最新更新:http://wordpress.org/extend/plugins/wp-gravatar-mini-cache/

[download id=”2″]

发表评论

textsms
account_circle
email

思章老师

WordPress头像缓存:WP Gravatar Mini Cache V1.0.4
近来博客联盟网站因备案临时关闭,我时间就多了,就利用这几天折腾一下WordPress,今天的新主题 LSZ Thinky Pro V3.1 准备启用头像缓存,以提升速度,向大家推荐WordPress头像缓存:WP Gr…
扫描二维码继续阅读
2011-04-02