I'm working on a plugin for a client and they need the ability to attach a couple of PDFs to a post as metadata. The client will need to occasionally update these attachments but without deleting the post itself. I have the upload/attachment working.
I've setup a custom post type to handle everything coming down from my plugin.
I've created two upload meta boxes (because I couldn't figure out multiple file support for a single upload meta box) and my uploads are working and the files are properly written to the database as metadata and saved used nonces.
I need to add the ability for the user to delete the current attachments and replace them using my simple uploader. This is where I'm lost. I really don't know how to approach it, using delete_post_meta, maybe? Anyway, here's the code. Any help is greatly appreciated!!!
/**
*Uploader 1
*
*
*/
function profoal_pdf_upload() {
add_meta_box('enicks_report', 'Enicks Report', 'enicks_report', 'broodmare', 'normal', 'high');
}
add_action('add_meta_boxes', 'profoal_pdf_upload');
function enicks_report() {
wp_nonce_field(plugin_basename(__FILE__), 'enicks_report_nonce');
$html = '<p class="description">';
$html .= 'Upload eNicks here';
$html .= '</p>';
$html .= '<input type="file" id="enicks_report" name="enicks_report" value="" size="25">';
echo $html;
}
add_action('save_post', 'save_custom_meta_data');
function save_custom_meta_data($id) {
if(!empty($_FILES['enicks_report']['name'])) {
$supported_types = array('application/pdf');
$arr_file_type = wp_check_filetype(basename($_FILES['enicks_report']['name']));
$uploaded_type = $arr_file_type['type'];
if(in_array($uploaded_type, $supported_types)) {
$upload = wp_upload_bits($_FILES['enicks_report']['name'], null, file_get_contents($_FILES['enicks_report']['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, 'enicks_report', $upload);
update_post_meta($id, 'enicks_report', $upload);
}
}
else {
wp_die("The file type that you've uploaded is not a PDF.");
}
}
}
/**
* Uploader 2
*
*/
function profoal_pedigree_upload() {
add_meta_box('pedigree', 'Pedigree', 'pedigree', 'broodmare', 'normal', 'high');
}
add_action('add_meta_boxes', 'profoal_pedigree_upload');
function pedigree() {
wp_nonce_field(plugin_basename(__FILE__), 'pedigree_nonce');
$html = '<p class="description">';
$html .= 'Upload pedigree here';
$html .= '</p>';
$html .= '<input type="file" id="pedigree" name="pedigree" value="" size="25">';
echo $html;
}
add_action('save_post', 'save_custom_pedigree_upload');
function save_custom_pedigree_upload($id) {
if(!empty($_FILES['pedigree']['name'])) {
$supported_types = array('application/pdf');
$arr_file_type = wp_check_filetype(basename($_FILES['pedigree']['name']));
$uploaded_type = $arr_file_type['type'];
if(in_array($uploaded_type, $supported_types)) {
$upload = wp_upload_bits($_FILES['pedigree']['name'], null, file_get_contents($_FILES['pedigree']['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, 'pedigree', $upload);
update_post_meta($id, 'pedigree', $upload);
}
}
else {
wp_die("The file type that you've uploaded is not a PDF.");
}
}
}
function update_edit_form() {
echo ' enctype="multipart/form-data"';
}
add_action('post_edit_form_tag', 'update_edit_form');
/**
* Display the attached pedigree & eNicks report
*
*/
function profoal_pdf_display() {
add_meta_box('display_pdf_attachments', 'Attached Reports', 'display_pdf_attachments', 'broodmare', 'normal', 'high');
}
add_action('add_meta_boxes', 'profoal_pdf_display');
function display_pdf_attachments () {
global $post;
wp_nonce_field(plugin_basename(__FILE__), 'display_pdf_attachments_nonce');
$pdf = get_post_meta($post->ID, 'enicks_report', true );
$pdf2 = get_post_meta($post->ID, 'pedigree', true);
echo '<p><b> Current eNicks Report:</b>' . $pdf['url'] . '</p>';
echo '<p><b>Current pedigree:</b>' . $pdf2['url'] . '</p>';
echo '<p> If you want to update a report, delete it, attached the new one above, and update the post.</p>';
}
update_post_meta
itself should help you