目前 WordPress 網站幾乎都是使用 Gravatar 全球通頭像來關聯使用者頭像的,但是由於 Gravatar 的伺服器是在國外,國內經常由於某些 XXX 原因而連線不上,今天就來教大家使用程式碼將 Gravatar 頭像半本地化, 那麼什麼是半本地化呢?也就是透過使用者的郵箱判斷使用者是否擁有 Gravatar 頭像,如果擁有則使用 Gravatar 頭像,當使用者擁有本地頭像且擁有 Gravatar 頭像時,則優先使用本地頭像。
- <?php
- class Simple_Local_Avatars {
- private $user_id_being_edited;
- public function __construct() {
- add_action( 'edit_user_profile', array( $this, 'edit_user_profile' ) );
- add_action( 'personal_options_update', array( $this, 'edit_user_profile_update' ) );
- }
- public function get_avatar( $avatar = '', $id_or_email, $size = 96, $default = '', $alt = false ) {
- if ( is_numeric($id_or_email) )
- $user_id = (int) $id_or_email;
- elseif ( is_string( $id_or_email ) && ( $user = get_user_by( 'email', $id_or_email ) ) )
- $user_id = $user->ID;
- elseif ( is_object( $id_or_email ) && ! emptyempty( $id_or_email->user_id ) )
- $user_id = (int) $id_or_email->user_id;
- if ( emptyempty( $user_id ) )
- return $avatar;
- $local_avatars = get_user_meta( $user_id, 'simple_local_avatar', true );
- if ( emptyempty( $local_avatars ) || emptyempty( $local_avatars['full'] ) )
- return $avatar;
- $size = (int) $size;
- if ( emptyempty( $alt ) )
- $alt = get_the_author_meta( 'display_name', $user_id );
- if ( emptyempty( $local_avatars[$size] ) ) {
- $upload_path = wp_upload_dir();
- $avatar_full_path = str_replace( $upload_path['baseurl'], $upload_path['basedir'], $local_avatars['full'] );
- $image_sized = image_resize( $avatar_full_path, $size, $size, true );
- $local_avatars[$size] = is_wp_error($image_sized) ? $local_avatars[$size] = $local_avatars['full'] : str_replace( $upload_path['basedir'], $upload_path['baseurl'], $image_sized );
- update_user_meta( $user_id, 'simple_local_avatar', $local_avatars );
- elseif ( substr( $local_avatars[$size], 0, 4 ) != 'http' ) {
- $local_avatars[$size] = home_url( $local_avatars[$size] );
- $author_class = is_author( $user_id ) ? ' current-author' : '' ;
- $avatar = "<img alt='" . esc_attr( $alt ) . "' src='" . $local_avatars[$size] . "' height='{$size}' width='{$size}' />";
- return apply_filters( 'simple_local_avatar', $avatar );
- public function admin_init() {
- register_setting( 'discussion', 'simple_local_avatars_caps', array( $this, 'sanitize_options' ) );
- }
- public function sanitize_options( $input ) {
- $new_input['simple_local_avatars_caps'] = emptyempty( $input['simple_local_avatars_caps'] ) ? 0 : 1;
- return $new_input;&nb
sp; - public function avatar_settings_field( $args ) {
- $options = get_option('simple_local_avatars_caps');
- echo '
- for="simple_local_avatars_caps">
- <input type="checkbox" name="simple_local_avatars_caps" id="simple_local_avatars_caps" value="1" ' . @checked( $options['simple_local_avatars_caps'], 1, false ) . ' />
- </label>
- }
- public function edit_user_profile( $profileuser ) {
- <h3><?php _e( '頭像','simple-local-avatars' ); ?></h3>
- <table class="form-table">
- <th><label for="simple-local-avatar"><?php _e('上傳頭像','simple-local-avatars'); ?></label></th>
- "width: 50px;" valign="top">
- <?php echo get_avatar( $profileuser->ID ); ?>
- <td>
- $options = get_option('simple_local_avatars_caps');
- if ( emptyempty($options['simple_local_avatars_caps']) || current_user_can('upload_files') ) {
- wp_nonce_field( 'simple_local_avatar_nonce', '_simple_local_avatar_nonce', false );
- <input type="file" name="simple-local-avatar" id="simple-local-avatar" /><br />
- if ( emptyempty( $profileuser->simple_local_avatar ) )
- echo '<span class="description">' . __('尚未設定本地頭像,請點選 「瀏覽」 按鈕上傳本地頭像。','simple-local-avatars') . '</span>';
- else
- echo '
- <input type="checkbox" name="simple-local-avatar-erase" value=span >"1" /> ' . __('移除本地頭像','simple-local-avatars') . '<br />
- class="description">' . __('如需要修改本地頭像,請重新上傳新頭像。如需要移除本地頭像,請選中上方的 「移除本地頭像」 核取方塊並更新個人資料即可。<br/> 移除本地頭像後,將恢復使用 Gravatar 頭像。','simple-local-avatars') . '</span>
- ';
- else {
- if ( emptyempty( $profileuser->simple_local_avatar ) )
- echo '<span class="description">' . __('尚未設定本地頭像,請在 Gravatar.com 網站設定頭像。','simple-local-avatars') . '</span>';
- else
- echo '<span class="description">' . __('你沒有頭像上傳全鄉,如需要修改本地頭像,請聯絡站點管理員。','simple-local-avatars') . '</span>';
- }
- </td>
- </table>
- "text/javascript">var form = document.getElementById('your-profile');form.encoding = 'multipart/form-data';form.setAttribute('enctype', 'multipart/form-data');</script>
- <?php
- public function edit_user_profile_update( $user_id ) {
- if ( ! isset( $_POST['_simple_local_avatar_nonce'] ) || ! wp_verify_nonce( $_POST['_simple_local_avatar_nonce'], 'simple_local_avatar_nonce' ) )
- return;
- if ( ! emptyempty( $_FILES['simple-local-avatar']['name'] ) ) {
- $mimes = array(
- 'gif' => 'image/gif',
- 'bmp' => 'image/bmp',
- );
- if ( ! function_exists( 'wp_handle_upload' ) )
- require_once( ABSPATH . 'wp-admin/includes/file.php' );
- $this->avatar_delete( $user_id );
- &
nbsp; - if ( strstr( $_FILES['simple-local-avatar']['name'], '.php' ) )
- wp_die('For security reasons, the extension ".php" cannot be in your file name.');
- $this->user_id_being_edited = $user_id;
- $avatar = wp_handle_upload( $_FILES['simple-local-avatar'], ar
ray( 'mimes' => $mimes, 'test_form' => false, 'unique_filename_callback' => array( $this, 'unique_filename_callback' ) ) ); - if ( emptyempty($avatar['file']) ) {
- switch ( $avatar['error'] ) {
- case 'File type does not meet security guidelines. Try another.' :
- add_action( 'user_profile_update_errors', create_function('$a','$a->add("avatar_error",__("請上傳有效的圖片檔案。","simple-local-avatars"));') );
- break;
- default :
- }
- return;
- $user_id, 'simple_local_avatar', array( 'full' => $avatar['url'] ) );
- } elseif ( ! emptyempty( $_POST['simple-local-avatar-erase'] ) ) {
- $this->avatar_delete( $user_id );
- }
- public function avatar_defaults( $avatar_defaults ) {
- return $avatar_defaults;
- public function avatar_delete( $user_id ) {
- $old_avatars = get_user_meta( $user_id, 'simple_local_avatar', true );
- $upload_path = wp_upload_dir();
- if ( is_array($old_avatars) ) {
- foreach ($old_avatars as $old_avatar ) {
- $old_avatar_path = str_replace( $upload_path['baseurl'], $upload_path['basedir'], $old_avatar );
- $old_avatar_path );
- }
- $user_id, 'simple_local_avatar' );
- }
- public function unique_filename_callback( $dir, $name, $ext ) {
- $user = get_user_by( 'id', (int) $this->user_id_being_edited );
- $name = $base_name = sanitize_file_name( substr(md5($user->user_login),0,12) . '_avatar' );
- $number = 1;
- while ( file_exists( $dir . "/$name$ext" ) ) {
- $name = $base_name . '_' . $number;
- $number++;
- }
- return $name . $ext;
- }
- $simple_local_avatars = new Simple_Local_Avatars;
- function get_simple_local_avatar( $id_or_email, $size = '96', $default = '', $alt = false ) {
- global $simple_local_avatars;
- $avatar = $simple_local_avatars->get_avatar( '', $id_or_email, $size, $default, $alt );
- if ( emptyempty ( $avatar ) )
- $avatar = get_avatar( $id_or_email, $size, $default, $alt );
- return $avatar;
- }
array( $this, 'get_avatar' ), 10, 5 );
array( $this, 'admin_init' ) );
array( $this, 'edit_user_profile' ) );
array( $this, 'edit_user_profile_update' ) );
array( $this, 'avatar_defaults' ) );
array( $this, 'avatar_settings_field' ), 'discussion', 'avatars' );
$a','$a->add("avatar_error","<strong>".__("上傳頭像過程中出現以下錯誤:","simple-local-avatars")."</strong> ' . esc_attr( $avatar['error'] ) . '");') );
array( $this, 'get_avatar' ) );
將以上程式碼加入到 functions.php 或者 functions.php 引入的 php 檔案中即可實現 Gravatar 頭像半本地化,最後來張效果圖:
