1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<form action="" method="post" enctype="multipart/form-data"> <input type="file" name="upload_attachment[]" class="files" size="50" multiple="multiple" /> <input type="submit" value="Submit Report" name="submit"> </form> <?php // Upload Multiple Image to Media Library in WordPress Front End if ($_FILES) { $files = $_FILES['upload_attachment']; foreach($files['name'] as $key => $value) { if ($files['name'][$key]) { $file = array( 'name' => $files['name'][$key], 'type' => $files['type'][$key], 'tmp_name' => $files['tmp_name'][$key], 'error' => $files['error'][$key], 'size' => $files['size'][$key] ); $_FILES = array( "upload_attachment" => $file ); foreach($_FILES as $file => $array) { echo $attachment_id = upload_user_file($array); } } } } ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?php // Upload Multiple Image to Media Library in WordPress Front End function upload_user_file($file = array()) { require_once (ABSPATH . 'wp-admin/includes/admin.php'); $file_return = wp_handle_upload($file, array( 'test_form' => false )); if (isset($file_return['error']) || isset($file_return['upload_error_handler'])) { return false; } else { $filename = $file_return['file']; $attachment = array( 'post_mime_type' => $file_return['type'], 'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)) , 'post_content' => '', 'post_status' => 'inherit', 'guid' => $file_return['url'] ); $attachment_id = wp_insert_attachment($attachment, $file_return['url']); require_once (ABSPATH . 'wp-admin/includes/image.php'); $attachment_data = wp_generate_attachment_metadata($attachment_id, $filename); wp_update_attachment_metadata($attachment_id, $attachment_data); if (0 < intval($attachment_id)) { return $attachment_id; } } return false; } ?> |
great! many thanks! 🙂