Unable to add Multiple files to a custom post type with the help of custom meta box

Hi I am trying to attach multiple document to a post (custom post type). but unable to add them, i do not want to try with any 3rd party plugin,i have googled and tried some code which i am sharing below, with the help of below i am able to upload single file but not multiple file to a post.

My Custom post type:

/* create custom post type for adding meeting */
add_action(‘init’, ‘meeting_schedule_init’);
function meeting_schedule_init(){
// set up product labels
register_post_type(‘Schedule Meeting’,array(
‘labels’ => array(
‘name’ => ‘Meetings’,
‘singular_name’ => ‘Meetings’,
‘add_new’ => ‘Add New Meeting’,
‘add_new_item’ => ‘Add New Meeting’,
‘edit_item’ => ‘Edit Meeting’,
‘new_item’ => ‘New Meeting’,
‘all_items’ => ‘All Meetings’,
‘view_item’ => ‘View Meetings’,
‘search_items’ => ‘Search Meetings’,
‘not_found’ => ‘No Meeting Schedule Found’,
‘not_found_in_trash’ => ‘No Meeting found in Trash’,
‘parent_item_colon’ => ”,
‘item_published’ => ‘Meeting has been Created’,
‘item_updated’ => ‘Meeting has been Updated’,
‘menu_name’ => ‘Meetings’,
),
‘public’ => true,
‘show_ui’ => true,
‘capability_type’ => ‘post’,
‘hierarchical’ => false,
‘rewrite’ => array(‘slug’ => ‘meetings’),
‘query_var’ => true,
‘menu_icon’ => ‘dashicons-megaphone’,
‘supports’ => array(
‘title’
)
)
);
}

My Custom meta-box:

function smt_register_meta_boxes() {
add_meta_box( ‘smt-1’, __( ‘Document Management’, ‘smt’ ), ‘smt_display_callback’ );
}
add_action( ‘add_meta_boxes’, ‘smt_register_meta_boxes’ );

/**
* Meta box display callback.
*
* @param WP_Post $post Current post object.
*/
function smt_display_callback( $post ) {
require_once ‘includes/view.php’;
}

my view.php:

<div class=”hcf_box”>
<style scoped>
.hcf_box{
display: grid;
grid-template-columns: max-content 1fr;
grid-row-gap: 10px;
grid-column-gap: 20px;
}
.hcf_field{
display: contents;
}
</style>
<?php wp_nonce_field(plugin_basename(__FILE__), ‘smt_document_attachment’); ?>
<p class=”meta-options hcf_field”>
<label for=”attachment”> Attach all your files here : </label>
<input id=”attachment” type=”file” name=”smt_multiple_attachments[]” multiple=”multiple” >
</p>

Adding mutlipart/form-data to my custom post type:

function update_edit_form() {
echo ‘ enctype=”multipart/form-data”‘;
} // end update_edit_form
add_action(‘post_edit_form_tag’, ‘update_edit_form’);

My Upload function:

function save_custom_meta_data($id) {
if ($_FILES[‘smt_multiple_attachments’][‘error’] !== UPLOAD_ERR_OK) __return_false();
// print_r($_FILES);exit;
require_once(ABSPATH . “wp-admin” . ‘/includes/image.php’);
require_once(ABSPATH . “wp-admin” . ‘/includes/file.php’);
require_once(ABSPATH . “wp-admin” . ‘/includes/media.php’);
// echo “<pre>”;
// print_r($_FILES[‘smt_multiple_attachments’]);exit;
// Make sure the file array isn’t empty
if(!empty($_FILES[‘smt_multiple_attachments’][‘name’])) {

// Setup the array of supported file types. In this case, it’s just PDF.
$supported_types = array(‘application/pdf’, ‘application/ppt’, ‘application/docx’);

// Get the file type of the upload
$arr_file_type = wp_check_filetype(basename($_FILES[‘smt_multiple_attachments’][‘name’]));
$uploaded_type = $arr_file_type[‘type’];

// Check if the type is supported. If not, throw an error.
if(in_array($uploaded_type, $supported_types)) {

// Use the WordPress API to upload the file
$upload = wp_upload_bits($_FILES[‘smt_multiple_attachments’][‘name’], null, file_get_contents($_FILES[‘smt_multiple_attachments’][‘tmp_name’]));

if(isset($upload[‘error’]) && $upload[‘error’] != 0) {
wp_die(‘There was an error uploading your file. The error is: ‘ . $upload[‘error’]);
} else {
add_post_meta($id, ‘smt_multiple_attachments’, $upload);
update_post_meta($id, ‘smt_multiple_attachments’, $upload);
} // end if/else

} else {
wp_die(“The file type that you’ve uploaded is not a PDF.”);
} // end if/else

} // end if

} // end save_custom_meta_data
add_action(‘save_post’, ‘save_custom_meta_data’);

Please suggest how to upload multiple document to custom post type with custom meta box as i tried.
Thanks

Read more here:: Unable to add Multiple files to a custom post type with the help of custom meta box

Leave a Reply

Your email address will not be published. Required fields are marked *