Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
    
Ahh I think I see. But instead of UNATTACHING my function would remove the metadata and delete the files right? –  drpcken Dec 3 '12 at 21:57
    
Yep, the wpse_54822_unattach PHP function is called by the Javascript. Execute what you need there, and you can also give a response back with a value. –  brasofilo Dec 3 '12 at 21:59
    
Thank you! I will research this. Definitely a step in the right direction. THANK YOU! –  drpcken Dec 3 '12 at 22:02
    
All of that is sort of overwhelming. I'm assuming since I've already created my DELETE button (that I can give a class or ID for jquery to reference) the only thing I need to do is create my function that runs the delete_post_meta and deletes the files right? And then write the jquery code to fire the function? –  drpcken Dec 3 '12 at 22:29
    
I think you should open a new Question, with your new working code. Because this one was about on how to execute a PHP function dispatched by an onClick event. And by now you probably have a completely different code. I'd also suggest that you publish it in full, or at least with all working relevant parts. –  brasofilo Dec 3 '12 at 23:14
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.