Hello! 欢迎来到盒子萌!

WordPress用QQ头像代替没有Gravatar头像


因为好多人在问我WordPress头像能不能用QQ头像替代,答案是可以对于没有Gravatar的人,用QQ头像代替Gravatar的,今天就发布在盒子萌博客上!具体代码是这样的

<?php

//首先要创建一个缓存文件夹ABSPATH . 'avatar/' 
//
function my_get_avatar($avatar, $id_or_email, $size) {
	
	// Get the author's email.
	$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) ) {
		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;
	}
	
	// Get the url of avatar
	$pattern = '/(?=http)[-\w:\/\.?&#;=]+/';
	$url_count = preg_match_all($pattern, $avatar, $original_avatar_url);
	$avatar_size = array();
	$avatar_url = array();
	$pattern = '/(?<=s=)\d+/';
	for ( $i = 0; $i < $url_count; ++$i) {		
		preg_match($pattern, $original_avatar_url[0][$i], $size_array);	
		$avatar_url[$i] = $original_avatar_url[0][$i];
		$avatar_size[$i] = $size_array[0];
	}
	
	// Check if the author has a gravatar.
	$hashkey = md5(strtolower(trim($email)));
	$test_url = 'http://www.gravatar.com/avatar/' . $hashkey . '?d=404';
	$data = wp_cache_get($hashkey);
	if ( false === $data ) {
		$response = wp_remote_head($test_url);
		if( is_wp_error($response) ) {
			$data = 'not200';
		} else {
			$data = $response['response']['code'];
		}
	    wp_cache_set($hashkey, $data, $group = '', $expire = 60*5);
	}
	
	if ( $data != '200' ) {		
		// The author doesn't have a gravatar.
		if ( stripos($email,"@qq.com") ) {
			// If he uses QQ mail, let WordPress use his QQ avatar instead.
			// Set the size of QQ avatar.
			for ( $i = 0; $i < $url_count; ++$i) {					
				if ( $avatar_size[$i] <= 100 ) $qq_avatar_size = 100;
				elseif ( $size <= 140 ) $qq_avatar_size = 140;
				elseif ( $size <= 240 ) $qq_avatar_size = 240;
				else $qq_avatar_size = 640;
				// q1.qlogo.cn, q3.qlogo.cn, q4.qlogo.cn also work.
				$avatar_url[$i] = 'http://q2.qlogo.cn/g?b=qq&nk=' . $email . '&s=' . $qq_avatar_size;
			}
		}
	}

	// Unfortunately I don't know what encrypt method the QQ avatar interface accepts.
	// So to protect the author's privacy, I have to cache the avatars.
	$wp_url = get_bloginfo('wpurl');
	// Caching.
	for ( $i = 0; $i < $url_count; ++$i) {
		$file_path = ABSPATH . 'avatar/' . $hashkey . '-' . $avatar_size[$i] . '.jpg';
		// 1209600s = 14d, the avatars will be cached for 2 weeks. You can change the period.
		$lifetime = 1209600; 
		if ( !is_file($file_path) || (time() - filemtime($file_path) ) > $lifetime) { 
			// If the file doesn't exist or it has been out of date, then update it.
			// It's necessary to use "-N --no-use-server-timestamps".
			exec("wget -N --no-use-server-timestamps -O '" . $file_path . "' '" . $avatar_url[$i] . "'  > /dev/null &");						
		}
		else $avatar = str_replace($original_avatar_url[0][$i], $wp_url . '/avatar/' . $hashkey . '-' . $avatar_size[$i] . '.jpg', $avatar);
		if ( filesize($file_path) < 500 ) copy($wp_url . '/avatar/default.jpg', $file_path);
	}
	return $avatar;	
}
add_filter( 'get_avatar', 'my_get_avatar', 10, 3);

?>

  • avatar
    游客

    博主问一下您,这个代码放到主题下的function.php吗?还有就是//首先要创建一个缓存文件夹ABSPATH . ‘avatar/’ //这个是要在根目录创建文件夹吗?能不能稍微详细的解答一下,一直不明白

    • avatar
      博主

      @ mr.zhu 要自己创建在根目录,至于路径你也可以创建到其他目录

      • avatar
        游客

        @ 专收爆米花 谢谢博主的回答,也就是说根目录创建avatar这个文件夹吗

        • avatar
          游客

          @ Mr.zhu 不用添加缓存文件夹直接用这个函数完美解决头像替换问题

          function use_qq_avatar_for_missing_gravatar($avatar, $id_or_email, $size, $default, $alt) {
          // 获取用户的邮箱
          if (is_numeric($id_or_email)) {
          $user = get_user_by(‘id’, $id_or_email);
          $email = $user->user_email;
          } elseif (is_object($id_or_email)) {
          $email = $id_or_email->comment_author_email;
          } else {
          $email = $id_or_email;
          }

          // 检查邮箱是否为 QQ 邮箱
          if (preg_match(‘/^[1-9][0-9]{4,10}@qq\.com$/i’, $email)) {
          // 提取 QQ 号码
          list($qq_number) = explode(‘@’, $email);

          // 构建 QQ 头像 URL
          $qq_avatar_url = “https://q1.qlogo.cn/g?b=qq&nk={$qq_number}&s=100”;

          // 没有 Gravatar 头像时使用 QQ 头像
          $avatar = “”;
          }

          return $avatar;
          }
          add_filter(‘get_avatar’, ‘use_qq_avatar_for_missing_gravatar’, 10, 5);

  • avatar
    游客

    然而并没有搞懂

  • avatar
    游客

    缓存文件夹是ABSPATH . ‘avatar/’吗
    那php文件名叫啥

  • avatar
    游客

    测试

  • avatar
    游客

    小白表示看不懂…..对于我来说有点难,如果有动态图或者视频就好了,我按照你评论区所说的改了,可是还是会出错。。。

  • avatar
    游客

    测试

  • avatar
    游客

    看不懂,得继续学习啊……

  • avatar
    游客

    这个写在该主题的那个文件能告知一下嘛

  • avatar
    游客

    suppport 支持

  • avatar
    游客

    学到了,再也不用单独设置了

  • avatar
    游客

    怎么设置?

    • avatar
      博主

      @ 懒人脸萌 这个…. 会玩的人才看的懂…代码修改,不是设置

发表评论

相关阅读