boxmoe_header_banner_img

Hello! 欢迎来到盒子萌!

加载中

文章导读

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


avatar
专收爆米花 2020年6月8日 10.35k

因为好多人在问我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);

?>


评论(18)

查看评论列表
评论头像
懒人脸萌 2020年06月11日
怎么设置?
评论头像
专收爆米花 博主 2020年06月11日
这个.... 会玩的人才看的懂...代码修改,不是设置
评论头像
咕咕咕 2020年06月13日
学到了,再也不用单独设置了
评论头像
nekoshiro 2020年06月28日
suppport 支持
评论头像
胡枫 2020年08月06日
这个写在该主题的那个文件能告知一下嘛
评论头像
专收爆米花 博主 2020年08月06日
functions.php
评论头像
老师你好我叫肖同学 2020年08月08日
看不懂,得继续学习啊……
评论头像
迷途 2021年05月13日
测试
评论头像
莫忘 2021年06月28日
小白表示看不懂.....对于我来说有点难,如果有动态图或者视频就好了,我按照你评论区所说的改了,可是还是会出错。。。
评论头像
专收爆米花 博主 2021年08月24日
结构不同,按报错修复就好
评论头像
荒野孤灯 2021年07月04日
测试
评论头像
活跃 2021年11月19日
缓存文件夹是ABSPATH . 'avatar/'吗 那php文件名叫啥
评论头像
专收爆米花 博主 2021年11月19日
是的 function.php
评论头像
网站领地 2021年12月09日
然而并没有搞懂
评论头像
mr.zhu 2024年10月30日
博主问一下您,这个代码放到主题下的function.php吗?还有就是//首先要创建一个缓存文件夹ABSPATH . 'avatar/' //这个是要在根目录创建文件夹吗?能不能稍微详细的解答一下,一直不明白
评论头像
专收爆米花 博主 2024年11月14日
要自己创建在根目录,至于路径你也可以创建到其他目录
评论头像
Mr.zhu 2024年11月14日
谢谢博主的回答,也就是说根目录创建avatar这个文件夹吗
评论头像
mr.zhu 2024年12月15日
不用添加缓存文件夹直接用这个函数完美解决头像替换问题 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);

发表评论

表情 颜文字
插入代码