Upload Multiple Files With media_handle_upload. Here if you use custom template past this in the begining:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php if( 'POST' == $_SERVER['REQUEST_METHOD'] ) { if ( $_FILES ) { $files = $_FILES["my_file_upload"]; 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 ("my_file_upload" => $file); foreach ($_FILES as $file => $array) { $newupload = my_handle_attachment($file,$pid); } } } } } ?> |
Add the below code in your in function.php
file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | function my_handle_attachment($file_handler,$post_id,$set_thu=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 ( is_numeric( $attach_id ) ) { update_post_meta( $post_id, '_my_file_upload', $attach_id ); } return $attach_id; } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.