0
我在前端做了一個自定義註冊表單。我想要顯示與後端相同的上傳功能。我不知道如何在前端添加多個上傳文件。 任何幫助真的很感激。wordpress前端多上傳與後端相同
我在前端做了一個自定義註冊表單。我想要顯示與後端相同的上傳功能。我不知道如何在前端添加多個上傳文件。 任何幫助真的很感激。wordpress前端多上傳與後端相同
<a href="#" class="custom_media_upload">Upload</a>
<img class="custom_media_image" src="" />
<input class="custom_media_url" type="text" name="attachment_url" value="">
<input class="custom_media_id" type="text" name="attachment_id" value="">
jQuery代碼:
$('.custom_media_upload').click(function() {
var send_attachment_bkp = wp.media.editor.send.attachment;
wp.media.editor.send.attachment = function(props, attachment) {
$('.custom_media_image').attr('src', attachment.url);
$('.custom_media_url').val(attachment.url);
$('.custom_media_id').val(attachment.id);
wp.media.editor.send.attachment = send_attachment_bkp;
}
wp.media.editor.open();
return false;
});
用戶insert_attachment wordpress保存附件,那麼你也將有功能得到不同風格的圖像,get_attachment(id,'full/medium/thumbnail')。
$files = $_FILES['upload_attachment'];
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,null);
// save the $newupload with you from data, as psot id for the attachment
}
function insert_attachment($file_handler,$post_id,$setthumb='false') {
// check to make sure its a successful upload
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$attach_id = media_handle_upload($file_handler, $post_id);
if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);
return $attach_id;
}
在HTML中,如果您提交表單,圖片將被上傳和referances將被保存在wp_post表
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="upload_attachment[]" id="file"><br>
<label for="file">Filename:</label>
<input type="file" name="upload_attachment[]" id="file"><br>
<label for="file">Filename:</label>
<input type="file" name="upload_attachment[]" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
我要找爲圖片上傳按鈕,以便我可以上傳圖片前端。 – user930026
添加html部分, –
這是完整的解決方案,將爲您提供從前端頁面上傳多個圖像的工具, –