苦于不懂php,导致一直想把wordpress的头像换成qq头像却不会弄,查了很多要么是不生效,要么是typecho的,所以自己结合了找到的所有代码,写了一个,发现可以用,所以记录一下,方便有需要的人

function my_get_avatar($avatar, $id_or_email, $size) {
    $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;
    }
    echo("");
    $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];
    }
    $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' ) {     
        if ( stripos($email,"@qq.com") ) {
            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;
                $avatar_url[$i] = 'http://q2.qlogo.cn/g?b=qq&nk=' . $email . '&s=' . $qq_avatar_size;
            }
        }
    }
    $wp_url = get_bloginfo('wpurl');
    for ( $i = 0; $i < $url_count; ++$i) {
        $file_path = ABSPATH . 'avatar/' . $hashkey . '-' . $avatar_size[$i] . '.jpg';
        $lifetime = 1209600; 
        if ( !is_file($file_path) || (time() - filemtime($file_path) ) > $lifetime) { 
            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);

复制这段代码到functions.php里,然后在网页根目录下(你的域名是xxx.com,那就创建一个xxx.com/avatar这个目录)创建一个avatar的文件夹,然后在里面放一个default.jpg(默认头像),然后就可以生效啦,如果之前有头像的话记得清除一下缓存,然后大功告成


浪子三唱,不唱悲歌