簡體   English   中英

Wordpress用戶個人資料中的自定義上傳字段

[英]Custom upload field in wordpress user profile

我試圖允許用戶在其用戶個人資料編輯頁面上上傳個人資料圖片,這是上傳表單的代碼:

function add_extra_profile_fields($user) {
    $output = '<h3>صورة المستخدم</h3>

    <table class="form-table">

        <tr>
            <th><label for="twitter">صورة المستخدم</label></th>

            <td>
                <img class="video_author_img" src="'.ms_user_img_single_php($user->ID, false).'" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                 <input type="file" id="admin_user_photo" name="admin_user_photo" class="form-text" /><br />
                <span class="description">لأفضل نتيجة يرجى استخدام صورة 90px في 90px  - الحجم الأقصى 2 ميجا</span>
            </td>
        </tr>

    </table>';

    echo $output;
}
add_action('edit_user_profile', 'add_extra_profile_fields');
add_action('show_user_profile', 'add_extra_profile_fields');

下一個功能應該是保存上載的圖像,並處理裁剪並將其與另一個功能放置在一起:

function my_save_extra_profile_fields( $user_id ) {

    if ( !current_user_can( 'edit_user', $user_id ) )
        return false;

    // User image upload
    $allowedTypes = array('image/gif', 'image/jpeg', 'image/png');

    if (in_array($_FILES["admin_user_photo"]["type"], $allowedTypes) && ($_FILES["admin_user_photo"]["size"] < 1048576)){

        ms_upload_crop_image('admin_user_photo', $user_id, '../../uploads/user_images/', 90, 90);

    }
}
add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );

我還使用jquery更改了表單的enctype:

$('form#your-profile').attr('enctype', 'multipart/form-data');

問題是瀏覽器甚至沒有上載該字段不存在的圖像,這是什么問題嗎?

您應該以個人資料形式更改兩個參數,而不是一個。 可能會有所幫助:

  form.encoding = "multipart/form-data";
  form.setAttribute('enctype', 'multipart/form-data');

我建議您為此目的使用現成的插件,稱為“添加本地頭像(http://wordpress.org/extend/plugins/add-local-avatar/)”,或者,如果您堅持使用自己的代碼,則只需下載插件並研究它的簡單源代碼。 比較它們並找到您的錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM