I have a custom post type of products that I’ve setup to upload PDF’s using metaboxes. When I generate the metaboxes on the wp-admin, I look to see if the metabox already has a pdf uploaded to it or not. If it DOES I generate a link to the pdf. If it doesn’t I generate a file upload control where they can pick a file to upload if they want. Here’s what my function looks like that generates this metadata.
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 PDF meta
$pdf_array = get_post_meta($post->ID, $field['id'], true);
//Grab the filename from the end of the path
$pdf_filename = substr(strrchr($pdf_array['url'], "/"), 1);
echo '<tr>',
'<td>';
//If Empty, show file upload dialog
if(empty($pdf_array)) {
echo '<input type="file" style="width: 700px;" name="', $field['id'], '" id="', $field['id'], '" />';
} else { //Else if pdf exists, show link to pdf and add DELETE button
echo '<li><a href="' . $pdf_array['url'] . '" target=_blank">'. $pdf_filename . '</a></li>';
}
echo '</td>',
'</tr>';
}
echo '</table>';
What I’d like to do is if there is a PDF metadata (meaning there’s a pdf already uploaded), I’d like to put a DELETE button next to the PDF Link.
} else { //Else if pdf exists, show link to pdf and add DELETE button
echo '<li><a href="' . $pdf_array['url'] . '" target=_blank">'. $pdf_filename . '</a></li>' ;
//I Want to add a delete button after the above echo
}
The delete button will remove all metadata from the PRODUCT post type, and delete the file off the server. SO I guess my question is how can I make a button that hooks into wordpress functions such as delete_post_meta
? I’m using wp_upload_bits()
to upload the pdf’s mostly because I don’t want the PDF’s added to the media library, just want to upload them to the server and get a path.
Thanks!
You are searching for Ajax.
This Q&A may give you an idea: Unattaching images from a post.
There are many specialists on the matter here at WPSE, check this search query.