站长资源模板插件教程 WordPress
wp头像加载不出来显示随机头像

wp头像随机显示qq头像

广告也精彩
/**
 * 为没有Gravatar头像的用户加载随机头像
 * @author INLOJV
 * @URI  https://www.inlojv.com/5459.html
 * 
 */
add_filter( 'get_avatar' , 'inlojv_custom_avatar' , 10 , 5 );
function inlojv_custom_avatar( $avatar, $id_or_email, $size, $default, $alt) {
 
		global $comment,$current_user;
		$avatar_host = "gravatar.loli.net/avatar";
		// $id_or_email的值:后台右上角登录用户头像为id,其他为邮箱,下面做一个判断
		$current_email =  is_int($id_or_email) ? get_user_by( 'ID', $id_or_email )->user_email : $id_or_email;
		
		$email = !empty($comment->comment_author_email) ? $comment->comment_author_email : $current_email ;
 	
 		if(strstr($email, '@qq.com')){
		    $qq=str_ireplace("@qq.com","",$email);
		    if(is_numeric($qq)&&strlen($qq)<11&&strlen($qq)>4){
    		    $url = 'https://s.p.qq.com/pub/get_face?img_type=3&uin='.$qq;
                $api = get_headers($url,true)['Location'];
                $json_api = json_encode($api);
                $ex_api = explode("&k=",$json_api)[1];
                $k_value = explode("&s=",$ex_api)[0];
                $headimg = 'https://q.qlogo.cn/g?b=qq&k='.$k_value.'&s=100';
		    }else{
		        $headimg = 'https://q1.qlogo.cn/g?b=qq&nk='.$qq.'&s=100';
		    }
		    $avatar = "<img src='{$headimg}' class='avatar avatar-32 photo' />";
		}else{
		$random_avatar_arr = array(
            '//tva1.sinaimg.com/square/6b002b97gy1ffs6ulm8ojj20690690sl.jpg',
			'//tva1.sinaimg.com/square/6b002b97gy1ffs6ulfp76j2069069t8p.jpg',
			'//tva1.sinaimg.com/square/6b002b97gy1ffs6ukuo5dj2069069a9w.jpg',
			'//tva1.sinaimg.com/square/6b002b97gy1ffs6ujijfoj206z05l746.jpg',
			'//tva1.sinaimg.com/square/6b002b97gy1ffs6uimd6zj207705edfr.jpg'
		);
 
		$email_hash = md5(strtolower(trim($email))); 
		$random_avatar = array_rand($random_avatar_arr,1);		
		$src = $random_avatar_arr[$random_avatar] ;		
		// JV提示:d参数404 onerror 方法 - 速度最快
		$avatar = "<img alt='{$alt}' src='//{$avatar_host}/{$email_hash}?d=404' onerror='javascript:this.src=\"{$src}\";this.onerror=null;' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
		
        // JV提示:d参数default_img 方法 - 速度稍逊
		// $src = urlencode( $src );
		//$avatar = "<img alt='{$alt}' src='//secure.gravatar.com/avatar/{$email_hash}?d={$src}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
		}
    return $avatar;
}

 

相关导航

广告也精彩

暂无评论

暂无评论...