2016-08-31 31 views

回答

2

爲了在WordPress的管理面板中更改用戶的個人資料照片,插件使用是最簡單最容易的選擇。

自定義用戶頭像

添加自定義的用戶個人資料照片到WordPress的用戶配置文件

https://wordpress.org/plugins/custom-user-profile-photo/

WP用戶頭像

使用從你的WordPress媒體的任何圖像庫作爲自定義用戶頭像。添加您自己的默認頭像。

https://wordpress.org/plugins/wp-user-avatar/

+0

@Will Nicholls。您是否按照建議使用插件按照問題中所述的要求獲得了您的輸出。 :)如果你覺得有什麼障礙與我分享,我會幫你。 –

1

默認情況下,WordPress使用的Gravatar基於與註冊的gravatar您的電子郵件ID顯示用戶的個人資料圖片。在控制面板中,WordPress擁有用戶個人資料頁面,其中包含多個用於輸入用戶數據的字段,但缺少用於添加自定義用戶頭像的圖像字段。

我們可以自定義頭像在下面的步驟:

第1步:添加腳本頁面。

在這一步中,我們將添加必要的Javascript到管理頁面。首先,我們將調用wp_enqueue_media,其中包含使用所有媒體JavaScript API所必需的所有腳本,樣式,設置和模板。

另一個腳本將用於在按鈕單擊時打開媒體上傳器並在DOM中插入附件ID。

請複製下面的腳本到一個文件並命名該文件作爲uploader.js

jQuery(document).ready(function() { 

    /* WP Media Uploader */ 
    var _shr_media = true; 
    var _orig_send_attachment = wp.media.editor.send.attachment; 

    jQuery('.shr-image').click(function() { 

     var button = jQuery(this), 
       textbox_id = jQuery(this).attr('data-id'), 
       image_id = jQuery(this).attr('data-src'), 
       _shr_media = true; 

     wp.media.editor.send.attachment = function(props, attachment) { 

      if (_shr_media && (attachment.type === 'image')) { 
       if (image_id.indexOf(",") !== -1) { 
        image_id = image_id.split(","); 
        $image_ids = ''; 
        jQuery.each(image_id, function(key, value) { 
         if ($image_ids) 
          $image_ids = $image_ids + ',#' + value; 
         else 
          $image_ids = '#' + value; 
        }); 

        var current_element = jQuery($image_ids); 
       } else { 
        var current_element = jQuery('#' + image_id); 
       } 

       jQuery('#' + textbox_id).val(attachment.id); 
           console.log(textbox_id) 
       current_element.attr('src', attachment.url).show(); 
      } else { 
       alert('Please select a valid image file'); 
       return false; 
      } 
     } 

     wp.media.editor.open(button); 
     return false; 
    }); 
}); 

現在,管理員添加諾斯腳本如下。

function shr_add_admin_scripts(){ 
     wp_enqueue_media(); 
     wp_enqueue_script('shr-uploader', get_stylesheet_directory_uri().'/js/uploader.js', array('jquery'), false, true); 
    } 
    add_action('admin_enqueue_scripts', 'shr_add_admin_scripts'); 

請注意,uploader.js保存在JS文件夾中的這個主題,所以你必須申請根據你的主題uploader.js的位置的正確路徑。

第2步:添加uploder按鈕來編輯配置文件頁面。

function shr_extra_profile_fields($user) { 

    $profile_pic = ($user!=='add-new-user') ? get_user_meta($user->ID, 'shr_pic', true): false; 

    if(!empty($profile_pic)){ 
     $image = wp_get_attachment_image_src($profile_pic, 'thumbnail'); 

    } ?> 

    <table class="form-table fh-profile-upload-options"> 
     <tr> 
      <th> 
       <label for="image"><?php _e('Main Profile Image', 'shr') ?></label> 
      </th> 

      <td> 
       <input type="button" data-id="shr_image_id" data-src="shr-img" class="button shr-image" name="shr_image" id="shr-image" value="Upload" /> 
       <input type="hidden" class="button" name="shr_image_id" id="shr_image_id" value="<?php echo !empty($profile_pic) ? $profile_pic : ''; ?>" /> 
       <img id="shr-img" src="<?php echo !empty($profile_pic) ? $image[0] : ''; ?>" style="<?php echo empty($profile_pic) ? 'display:none;' :'' ?> max-width: 100px; max-height: 100px;" /> 
      </td> 
     </tr> 
    </table><?php 

} 
add_action('show_user_profile', 'shr_extra_profile_fields'); 
add_action('edit_user_profile', 'shr_extra_profile_fields'); 
add_action('user_new_form', 'shr_extra_profile_fields'); 

在上面的代碼中,show_user_profile,edit_user_profile和user_new_form鉤用於添加的上傳按鈕,這樣該按鈕將是現有用戶的個人資料頁面,以及創建新用戶時可見。

輸入按鈕是點擊打開WordPress媒體上傳器。 輸入隱藏字段是存儲來自WordPress的媒體上傳器的不受干擾或選定圖像的附件標識。

步驟3:將附件圖片ID保存到WordPress的usermeta表中。

WordPress中的Usermeta表格是爲了存儲與用戶有關的額外信息,在這裏我們將存儲用戶的圖像附件標識。使用該附件ID,我們可以獲取有關圖像的所有數據。

爲了保存附件ID,我們將使用profile_update和user_register鉤子,當創建任何新用戶或更新現有用戶時將會觸發這些鉤子。

function shr_profile_update($user_id){ 

    if(current_user_can('edit_users')){ 
     $profile_pic = empty($_POST['shr_image_id']) ? '' : $_POST['shr_image_id']; 
     update_user_meta($user_id, 'shr_pic', $profile_pic); 
    } 

} 
add_action('profile_update', 'shr_profile_update'); 
add_action('user_register', 'shr_profile_update'); 

就是這樣,你已經成功地將個人資料相片上傳按鈕在WordPress的儀表板,個人資料頁。

參考:http://sharethingz.com/wordpress/custom-user-avatar-in-wordpress/

相關問題