Adding pdf upload to custom post type, strange behavior checking input control
So I have a custom post type I made called PRODUCTS. It has metaboxes for Description, and PDF Uploads (currently only 2 pdf's). The description metaboxes are updating properly and not a problem. I recently tried adding metaboxes for the PDF uploads and have had nothing but problems. I though the problem was with my upload functions but it turns out when I'm checking to see if my file input control has a value or not, it acts weird. Here's the code for my Metaboxes for Products:
//* Add custom Meta Boxes for Products *//
$prefix = 'aps_'; //To prevent conflicts with other plugins
//Description Meta Box
$meta_box_description = array(
'id' => 'products-desc-meta-boxes',
'title' => "Product Description",
'page' => 'product', //attach to products custom post
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Description',
'desc' => 'Product Description',
'id' => $prefix . 'description',
'type' => 'text',
'std' => '' //Default Value
)
)
);
//PDF upload Meta Boxes
$meta_box_pdf_uploads = array(
'id' => 'products-pdf-meta-boxes',
'title' => "PDFs",
'page' => 'product', //attach to products custom post
'context' => 'normal',
'priority' => 'default',
'fields' => array(
array(
'name' => $prefix . 'PDF1',
'desc' => 'Product PDF',
'id' => $prefix . 'pdf1',
'type' => 'text'
),
array(
'name' => $prefix . 'PDF2',
'desc' => 'Product PDF',
'id' => $prefix . 'pdf2',
'type' => 'text'
)
)
);
Since the description meta boxes are updating properly, I'll just show you where I'm creating the metaboxes on the admin page for the PDF upload inputs: function products_pdf_uploads_show_meta() { global $meta_box_pdf_uploads, $post, $prefix;
echo '<table class="form-table">';
echo '<p class="description">Upload your PDFs here</p>';
foreach ($meta_box_pdf_uploads['fields'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);
echo '<tr>',
'<td>',
'<input type="file" style="width: 700px;" name="', $field['id'], '" id="', $field['id'], '" />',
'</td>',
'</tr>';
}
echo '</table>';
}
This function does as expected and generates two Input fields since I only have two PDF fields specified in my metabox declaration.
Now when I try to save my Product, the description get updated fine, but the pdf's do not upload. I figured out its because I'm checking if the File Upload Control isset. Even when it has a file chosen (pressing browse and picking a PDF), it still thinks it's empty. I figured this out by debugging with wp_die. Here's my code that add/updates the Product:
//Save data from meta box
function products_save_data( $post_id ) {
global $post, $meta_box_description, $meta_box_pdf_uploads;
// verify nonce
if (!wp_verify_nonce($_POST['products_meta_box_nonce'], basename(__FILE__))) {
return $post_id;
}
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
// check permissions
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id)) {
return $post_id;
}
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
//Save Description Field
foreach ($meta_box_description['fields'] as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
}
//Upload PDF files
foreach ($meta_box_pdf_uploads['fields'] as $field) {
//Make sure field isn't emtpy
if(isset($_FILES[$field['name']])) {
wp_die('Passed! '. $_FILES[$field['name']]); //debugging
//Setup File type allowed
$supported_file_type = array('application/pdf');
//Get the file type
$file_type = wp_check_filetype(basename($_FILES[$field['id']]));
$uploaded_type = $file_type('type');
//Check if FileType is allowed
if(in_array($uploaded_type, $supported_file_type)) {
//Use API to upload file
$uploaded_file = wp_upload_bits($_FILES[$field['id']], null, file_get_contents($_FILES[$field['name']]));
//If error is thrown on upload, die
if(isset($uploaded_file['error']) && $uploaded_file['error'] != 0) {
wp_die('There was an error uploading your PDF. The error is: ' . $uploaded_file['error']);
} else { //upload pdf
add_post_meta($post_id, $field['id'], $uploaded_file);
update_post_meta($post_id, $field['id'], $uploaded_file);
}
} else { //file type not supported
wp_die("The file type you have uploaded is not a PDF.");
}
} else {
wp_die('Nothing was passed: ' . print_r($_FILES[$field['id']]) ); //debugging
}
}
}
The problem I'm having is when I'm checking if the PDF input field isset, even if it is, it says it isn't. It skips to the ELSE Statement and tells me "NOTHING IS PASSED". Am I checking the input field incorrectly? if(isset($_FILES[$field['name']])
is that the correct field['name'] to be checking? This has really confused and frustrated me for a whole day.
Any help or a push in the right direction would be greatly appreciated! Thank you!
Posted Dec 2, 2012, 5:48 PM UTC · edited