問題描述

我正在嘗試做類似於上述問題的事情。我試圖讓使用者釋出和上傳 front-end 的影像。我已經完成了職位表格和工作。

我剛剛跟著嘗試了羅賓·奈特 upload-post-thumbnail-from-the-front-end 釋出的答案。可悲的是,我不能讓它上班。有什麼我想要改變還是編輯?

謝謝。

最佳解決方案

如果您正在談論答案,我釋出了 here 它只需在 iframe 中上傳檔案即可實現”Ajax like” 提交。

現在,如果您已經有一個處理釋出提交的表單,您可以直接在您的表單中新增上傳檔案欄位輸入:

<form ...
...
<input type="file" name="thumbnail" id="thumbnail">
...
...
</form>

確保您的表單具有 enctype="multipart/form-data"屬性。

然後在您建立帖子後的表單處理指令碼 (假設您正在使用 wp_insert_post();) 在新的 var 中保持帖子 ID:

$new_post = wp_insert_post($post_array);

之後新增:

            if (!function_exists('wp_generate_attachment_metadata')){
                require_once(ABSPATH . "wp-admin" . '/includes/image.php');
                require_once(ABSPATH . "wp-admin" . '/includes/file.php');
                require_once(ABSPATH . "wp-admin" . '/includes/media.php');
            }
             if ($_FILES) {
                foreach ($_FILES as $file => $array) {
                    if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
                        return "upload error : " . $_FILES[$file]['error'];
                    }
                    $attach_id = media_handle_upload( $file, $new_post );
                }
            }
            if ($attach_id > 0){
                //and if you want to set that image as Post  then use:
                update_post_meta($new_post,'_thumbnail_id',$attach_id);
            }

您的影像將被上傳並儲存為帖子縮圖。

次佳解決方案

我建議你在這裡看看 @Goldenapples 的解決方案,http://goldenapplesdesign.com/2010/07/03/front-end-file-uploads-in-wordpress/

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。