Single

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

缓存文件夹是ABSPATH . ‘avatar/’

function.php

  1. <?php
  2.  
  3. //首先要创建一个缓存文件夹ABSPATH . ‘avatar/’
  4. //
  5. function my_get_avatar($avatar, $id_or_email, $size) {
  6. // Get the author’s email.
  7. $email = ;
  8. if ( is_numeric($id_or_email) ) {
  9. $id = (int) $id_or_email;
  10. $user = get_userdata($id);
  11. if ( $user )
  12. $email = $user->user_email;
  13. } elseif ( is_object($id_or_email) ) {
  14. if ( !empty($id_or_email->user_id) ) {
  15. $id = (int) $id_or_email->user_id;
  16. $user = get_userdata($id);
  17. if ( $user)
  18. $email = $user->user_email;
  19. } elseif ( !empty($id_or_email->comment_author_email) ) {
  20. $email = $id_or_email->comment_author_email;
  21. }
  22. } else {
  23. $email = $id_or_email;
  24. }
  25. // Get the url of avatar
  26. $pattern = ‘/(?=http)[-\w:\/\.?&#;=]+/’;
  27. $url_count = preg_match_all($pattern, $avatar, $original_avatar_url);
  28. $avatar_size = array();
  29. $avatar_url = array();
  30. $pattern = ‘/(?<=s=)\d+/’;
  31. for ( $i = 0; $i < $url_count; ++$i) {
  32. preg_match($pattern, $original_avatar_url[0][$i], $size_array);
  33. $avatar_url[$i] = $original_avatar_url[0][$i];
  34. $avatar_size[$i] = $size_array[0];
  35. }
  36. // Check if the author has a gravatar.
  37. $hashkey = md5(strtolower(trim($email)));
  38. $test_url = ‘http://www.gravatar.com/avatar/’ . $hashkey . ‘?d=404’;
  39. $data = wp_cache_get($hashkey);
  40. if ( false === $data ) {
  41. $response = wp_remote_head($test_url);
  42. if( is_wp_error($response) ) {
  43. $data = ‘not200’;
  44. } else {
  45. $data = $response[‘response’][‘code’];
  46. }
  47. wp_cache_set($hashkey, $data, $group = , $expire = 60*5);
  48. }
  49. if ( $data != ‘200’ ) {
  50. // The author doesn’t have a gravatar.
  51. if ( stripos($email,“@qq.com”) ) {
  52. // If he uses QQ mail, let WordPress use his QQ avatar instead.
  53. // Set the size of QQ avatar.
  54. for ( $i = 0; $i < $url_count; ++$i) {
  55. if ( $avatar_size[$i] <= 100 ) $qq_avatar_size = 100;
  56. elseif ( $size <= 140 ) $qq_avatar_size = 140;
  57. elseif ( $size <= 240 ) $qq_avatar_size = 240;
  58. else $qq_avatar_size = 640;
  59. // q1.qlogo.cn, q3.qlogo.cn, q4.qlogo.cn also work.
  60. $avatar_url[$i] = ‘http://q2.qlogo.cn/g?b=qq&nk=’ . $email . ‘&s=’ . $qq_avatar_size;
  61. }
  62. }
  63. }
  64.  
  65. // Unfortunately I don’t know what encrypt method the QQ avatar interface accepts.
  66. // So to protect the author’s privacy, I have to cache the avatars.
  67. $wp_url = get_bloginfo(‘wpurl’);
  68. // Caching.
  69. for ( $i = 0; $i < $url_count; ++$i) {
  70. $file_path = ABSPATH . ‘avatar/’ . $hashkey . ‘-‘ . $avatar_size[$i] . ‘.jpg’;
  71. // 1209600s = 14d, the avatars will be cached for 2 weeks. You can change the period.
  72. $lifetime = 1209600;
  73. if ( !is_file($file_path) || (time() filemtime($file_path) ) > $lifetime) {
  74. // If the file doesn’t exist or it has been out of date, then update it.
  75. // It’s necessary to use “-N –no-use-server-timestamps”.
  76. exec(“wget -N –no-use-server-timestamps -O ‘” . $file_path . “‘ ‘” . $avatar_url[$i] . “‘ > /dev/null &”);
  77. }
  78. else $avatar = str_replace($original_avatar_url[0][$i], $wp_url . ‘/avatar/’ . $hashkey . ‘-‘ . $avatar_size[$i] . ‘.jpg’, $avatar);
  79. if ( filesize($file_path) < 500 ) copy($wp_url . ‘/avatar/default.jpg’, $file_path);
  80. }
  81. return $avatar;
  82. }
  83. add_filter( ‘get_avatar’, ‘my_get_avatar’, 10, 3);
  84.  
  85. ?>

 

 

暂无评论

发表评论