問題描述
我正在嘗試將影像上傳器新增到使用者個人資料頁面。我確實有一個基本的註冊頁面執行,但不知何故相同,但稍微調整的程式碼在新增到 functions.php 時不起作用。該欄位被載入,並且 WordPress 確實允許容易的更新,但在配置檔案檢視中沒有顯示影像。要獲取影像並儲存我使用
/********************************************************************
Profile Image Upload Field Code
*********************************************************************/
require_once( STYLESHEETPATH . '/img_upload_resize_crop.php' );
add_action( 'init', 'custom_img_uploader' );
function custom_img_uploader () {
if (isset($_FILES['profilePicture']['name'])!= "" ) {
if ( $_FILES['profilePicture']['name']!="" ) {
$your_image = new _image;
$upload_dir = wp_upload_dir();
//To Upload
$your_image->uploadTo = $upload_dir['basedir'].'/';
$upload = $your_image->upload($_FILES['profilePicture']);
//To Resize
$your_image->newPath = $upload_dir['basedir'].'/thumbs/';
$your_image->newWidth = 150;
$your_image->newHeight = 200;
$resized = $your_image->resize();
$profilePicture=str_replace($upload_dir['basedir'].'/thumbs/', "", $resized );
unlink($upload);
}else{
$profilePicture='';
}
}
}
要顯示 HTML 中的欄位我有:
<!-- begin image uploader field -->
<tr>
<th><label for="profilePicture"><?php _e("Profile Image"); ?></label></th>
<td>
<input type="file" name="profilePicture" id="profilePicture" style="float:left;" />
</td>
</tr>
並儲存它,我用這個:
update_user_meta( $user_id, 'userphoto_thumb_height', 59);
update_user_meta( $user_id, 'userphoto_thumb_width', 80 );
update_user_meta( $user_id, 'userphoto_thumb_file', $profilePicture );
有人看到為什麼在配置檔案中檢視此程式碼:
<?php if(get_the_author_meta( 'userphoto_thumb_file', $getId )!=""){?>
<img src="<?=$uploads['baseurl'];?>/thumbs/<?=get_the_author_meta(%20'userphoto_thumb_file',%20$getId%20);?>" />
不載入儲存的影像?
更新一和二
基本除錯
更新 III 新增 isset()
後,我有點進一步:
[28-Mar-2012 07:14:46] PHP Notice: Undefined variable: profilePicture in /home/user/public_html/wp-content/themes/theme-name/functions.php on line 346
在這一行我有 update_user_meta( $user_id, 'userphoto_thumb_file', $profilePicture );
似乎,即使我有變數
profilePicture
定義,它不被抓住只是一個通知,似乎沒有相關,也不會導致上傳問題。
更新 IV
我將影像上傳器程式碼包裝在一個函式中,並將其新增為一個操作。請參閱上面的更新程式碼。我還有一個未定義的變數 profilePicture
也不是上傳的影像。整個 functions.php 檔案在這裡 http://pastebin.com/YGYz3Qnv
更新 V
我只是意識到我沒有一個很好的 $ _POST 選項的 profilePicture 。看看在 http://plugins.svn.wordpress.org/user-avatar/trunk/user-avatar.php 的 @brasofilo 提到的外掛我看到這個程式碼我可以使用,但我需要調整很多:
<form enctype="multipart/form-data" id="uploadForm" method="POST" action="<?php echo admin_url('admin-ajax.php'); ?>?action=user_avatar_add_photo&step=2&uid=<?php echo $uid; ?>" >
<label for="upload"><?php _e('Choose an image from your computer:','user-avatar'); ?></label><br /><input type="file" id="upload" name="uploadedfile" />
<input type="hidden" name="action" value="save" />
<?php wp_nonce_field('user-avatar') ?>
<p class="submit"><input type="submit" value="<?php esc_attr_e('Upload'); ?>" /></p>
</form>
我只是想知道是否需要一個表單,因為它不是其他文字欄位,以及我需要新增 $user_id
和如何調整 update_user_meta
更新 VI
根據貢獻者的推薦,製作全球 $profilePicture
更新七
考慮使用來自無關媒體 here 的選項。但是,由於我將時間花費在我現在的 functions.php 中,所以我會保持這個執行緒開啟,直到我設法完成這個。
最佳解決方案
感謝大家的建議。最後,我去了 Woothemes Media Uploader 整合,你可以在這裡看到 Github:https://github.com/devinsays/options-framework-plugin/blob/master/options-medialibrary-uploader.php 它被整合到一個主題框架中,我將整合在我自己的主題中,這樣我就可以用一隻石頭殺死兩隻鳥。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。