目前 WordPress 网站几乎都是使用 Gravatar 全球通头像来关联用户头像的,但是由于 Gravatar 的服务器是在国外,国内经常由于某些 XXX 原因而连接不上,今天就来教大家使用代码将 Gravatar 头像半本地化, 那么什么是半本地化呢?也就是通过用户的邮箱判断用户是否拥有 Gravatar 头像,如果拥有则使用 Gravatar 头像,当用户拥有本地头像且拥有 Gravatar 头像时,则优先使用本地头像。

  1. <?php  
  2. class Simple_Local_Avatars {  
  3.     private $user_id_being_edited;  
  4.     public function __construct() {  
  5. array$this, 'get_avatar' ), 10, 5 );  

  6.           
  7. array$this, 'admin_init' ) );  

  8.           
  9. array$this, 'edit_user_profile' ) );  

  10.         add_action( 'edit_user_profile', array$this, 'edit_user_profile' ) );  
  11.         add_action( 'personal_options_update', array$this, 'edit_user_profile_update' ) );  
  12. array$this, 'edit_user_profile_update' ) );  

  13.           
  14. array$this, 'avatar_defaults' ) );  

  15.     }  
  16.     public function get_avatar( $avatar = ''$id_or_email$size = 96, $default = ''$alt = false ) {  
  17.         if ( is_numeric($id_or_email) )  
  18. $user_id = (int) $id_or_email;  
  19.         elseif ( is_string$id_or_email ) && ( $user = get_user_by( 'email', $id_or_email ) ) )  
  20. $user_id = $user->ID;  
  21.         elseif ( is_object$id_or_email ) && ! emptyempty$id_or_email->user_id ) )  
  22. $user_id = (int) $id_or_email->user_id;  
  23.           
  24. if ( emptyempty$user_id ) )  
  25.             return $avatar;  
  26.         $local_avatars = get_user_meta( $user_id, 'simple_local_avatar', true );  
  27.         if ( emptyempty$local_avatars ) || emptyempty$local_avatars['full'] ) )  
  28. return $avatar;  
  29.           
  30. $size = (int) $size;  
  31.           
  32. if ( emptyempty$alt ) )  
  33.             $alt = get_the_author_meta( 'display_name', $user_id );  
  34.           
  35. if ( emptyempty$local_avatars[$size] ) ) {  
  36.             $upload_path = wp_upload_dir();  
  37. $avatar_full_path = str_replace$upload_path['baseurl'], $upload_path['basedir'], $local_avatars['full'] );  
  38.             $image_sized = image_resize( $avatar_full_path$size$size, true );        
  39.             $local_avatars[$size] = is_wp_error($image_sized) ? $local_avatars[$size] = $local_avatars['full'] : str_replace$upload_path['basedir'], $upload_path['baseurl'], $image_sized );  
  40.             update_user_meta( $user_id, 'simple_local_avatar', $local_avatars );  
  41. elseif ( substr$local_avatars[$size], 0, 4 ) != 'http' ) {  
  42.             $local_avatars[$size] = home_url( $local_avatars[$size] );  
  43.           
  44. $author_class = is_author( $user_id ) ? ' current-author' : '' ;  
  45.         $avatar = "<img alt='" . esc_attr( $alt ) . "' src='" . $local_avatars[$size] . "'  height='{$size}' width='{$size}' />";  
  46.         return apply_filters( 'simple_local_avatar', $avatar );  
  47.           
  48. public function admin_init() {  
  49.           
  50.         register_setting( 'discussion', 'simple_local_avatars_caps', array$this, 'sanitize_options' ) );  
  51. array$this, 'avatar_settings_field' ), 'discussion', 'avatars' );  

  52.     }  
  53.     public function sanitize_options( $input ) {  
  54. $new_input['simple_local_avatars_caps'] = emptyempty$input['simple_local_avatars_caps'] ) ? 0 : 1;  
  55.         return $new_input;&nb
    sp; 
  56.           
  57. public function avatar_settings_field( $args ) {         
  58.         $options = get_option('simple_local_avatars_caps');  
  59.         echo '  
  60. for="simple_local_avatars_caps">  
  61.                 <input type="checkbox" name="simple_local_avatars_caps" id="simple_local_avatars_caps" value="1" ' . @checked( $options['simple_local_avatars_caps'], 1, false ) . ' />  
  62.             </label>  
  63.     }  
  64.     public function edit_user_profile( $profileuser ) {  
  65.     <h3><?php _e( '头像','simple-local-avatars' ); ?></h3>  
  66.     <table class="form-table">  
  67.             <th><label for="simple-local-avatar"><?php _e('上传头像','simple-local-avatars'); ?></label></th>  
  68. "width: 50px;" valign="top">  
  69.                 <?php echo get_avatar( $profileuser->ID ); ?>  
  70.             <td>  
  71.                 $options = get_option('simple_local_avatars_caps');  
  72.                 if ( emptyempty($options['simple_local_avatars_caps']) || current_user_can('upload_files') ) {  
  73.                     wp_nonce_field( 'simple_local_avatar_nonce', '_simple_local_avatar_nonce', false );  
  74.                     <input type="file" name="simple-local-avatar" id="simple-local-avatar" /><br />  
  75.                     if ( emptyempty$profileuser->simple_local_avatar ) )  
  76. echo '<span class="description">' . __('尚未设置本地头像,请点击 「浏览」 按钮上传本地头像。','simple-local-avatars') . '</span>';  
  77.                     else  
  78. echo '  
  79.                             <input type="checkbox" name="simple-local-avatar-erase" value=span >"1" /> ' . __('移除本地头像','simple-local-avatars') . '<br />  
  80. class="description">' . __('如需要修改本地头像,请重新上传新头像。如需要移除本地头像,请选中上方的 「移除本地头像」 复选框并更新个人资料即可。<br/> 移除本地头像后,将恢复使用 Gravatar 头像。','simple-local-avatars') . '</span>  
  81.                         ';       
  82. else {  
  83.                     if ( emptyempty$profileuser->simple_local_avatar ) )  
  84. echo '<span class="description">' . __('尚未设置本地头像,请在 Gravatar.com 网站设置头像。','simple-local-avatars') . '</span>';  
  85.                     else  
  86. echo '<span class="description">' . __('你没有头像上传全乡,如需要修改本地头像,请联系站点管理员。','simple-local-avatars') . '</span>';  
  87.                 }  
  88.             </td>  
  89.     </table>  
  90. "text/javascript">var form = document.getElementById('your-profile');form.encoding = 'multipart/form-data';form.setAttribute('enctype', 'multipart/form-data');</script>  
  91.     <?php         
  92.           
  93. public function edit_user_profile_update( $user_id ) {  
  94.         if ( ! isset( $_POST['_simple_local_avatar_nonce'] ) || ! wp_verify_nonce( $_POST['_simple_local_avatar_nonce'], 'simple_local_avatar_nonce' ) )              
  95. return;  
  96.           
  97. if ( ! emptyempty$_FILES['simple-local-avatar']['name'] ) ) {  
  98.             $mimes = array(  
  99.                 'gif' => 'image/gif',  
  100.                 'bmp' => 'image/bmp',  
  101.             );  
  102.               
  103. if ( ! function_exists( 'wp_handle_upload' ) )  
  104.                 require_once( ABSPATH . 'wp-admin/includes/file.php' );  
  105.             $this->avatar_delete( $user_id );      
  106.        &
    nbsp;      
  107. if ( strstr$_FILES['simple-local-avatar']['name'], '.php' ) )  
  108.                 wp_die('For security reasons, the extension ".php" cannot be in your file name.');  
  109.             $this->user_id_being_edited = $user_id  
  110. $avatar = wp_handle_upload( $_FILES['simple-local-avatar'], ar
    ray( 'mimes' => $mimes, 'test_form' => false, 'unique_filename_callback' => array$this, 'unique_filename_callback' ) ) );  
  111.           
  112. if ( emptyempty($avatar['file']) ) {       
  113.                 switch ( $avatar['error'] ) {  
  114. case 'File type does not meet security guidelines. Try another.' :  
  115.                         add_action( 'user_profile_update_errors', create_function('$a','$a->add("avatar_error",__("请上传有效的图片文件。","simple-local-avatars"));') );                
  116. break;  
  117.                     default :  
  118. $a','$a->add("avatar_error","<strong>".__("上传头像过程中出现以下错误:","simple-local-avatars")."</strong> ' . esc_attr( $avatar['error'] ) . '");') );  

  119.                 }  
  120.                 return;  
  121.           
  122. $user_id, 'simple_local_avatar', array( 'full' => $avatar['url'] ) );        
  123.         } elseif ( ! emptyempty$_POST['simple-local-avatar-erase'] ) ) {  
  124. $this->avatar_delete( $user_id );  
  125.         }  
  126.           
  127.  
  128.     public function avatar_defaults( $avatar_defaults ) {  
  129. array$this, 'get_avatar' ) );  

  130.         return $avatar_defaults;  
  131.           
  132.  
  133.     public function avatar_delete( $user_id ) {  
  134. $old_avatars = get_user_meta( $user_id, 'simple_local_avatar', true );  
  135.         $upload_path = wp_upload_dir();  
  136.         if ( is_array($old_avatars) ) {  
  137. foreach ($old_avatars as $old_avatar ) {  
  138.                 $old_avatar_path = str_replace$upload_path['baseurl'], $upload_path['basedir'], $old_avatar );  
  139. $old_avatar_path );     
  140.             }  
  141.           
  142. $user_id, 'simple_local_avatar' );  
  143.     }  
  144.     public function unique_filename_callback( $dir$name$ext ) {  
  145. $user = get_user_by( 'id', (int) $this->user_id_being_edited );  
  146.         $name = $base_name = sanitize_file_name( substr(md5($user->user_login),0,12) . '_avatar' );  
  147. $number = 1;  
  148.           
  149. while ( file_exists$dir . "/$name$ext" ) ) {  
  150.             $name = $base_name . '_' . $number;  
  151. $number++;  
  152.         }  
  153.         return $name . $ext;  
  154. }  
  155. $simple_local_avatars = new Simple_Local_Avatars;  
  156. function get_simple_local_avatar( $id_or_email$size = '96', $default = ''$alt = false ) {  
  157. global $simple_local_avatars;  
  158.     $avatar = $simple_local_avatars->get_avatar( ''$id_or_email$size$default$alt );  
  159.     if ( emptyempty ( $avatar ) )  
  160. $avatar = get_avatar( $id_or_email$size$default$alt );  
  161.           
  162. return $avatar;  
  163. }  

将以上代码加入到 functions.php 或者 functions.php 引入的 php 文件中即可实现 Gravatar 头像半本地化,最后来张效果图: