0

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>';
    }    
1
  • update_post_meta itself should help you
    – WPTC-Troop
    Dec 16, 2015 at 14:44

1 Answer 1

0

You have add_post_meta() and update_post_meta(), but you only need the latter:

This may be used in place of add_post_meta() function. The first thing this function will do is make sure that $meta_key already exists on $post_id. If it does not, add_post_meta($post_id, $meta_key, $meta_value) is called instead and its result is returned.

What you are doing now with both functions back to back is pointless and is causing double writes to the database.

Second, assuming that the rest of your code works, update_post_meta() itself should overwrite any previous data for the post ID and key in question. You don't need to explicitly delete the value. The only time you'd need more complicated logic would be if you wanted to delete data but not replace it, and that doesn't appear to be what you are trying to do (though I could be wrong). I'd add an explicit "delete data" checkbox to the form for that case, then use delete_post_meta().

1
  • Following your advice, I removed add_post_meta() and then tested my uploads and sure enough, the newly added files overwrote the previously uploaded data. So, this worked! Thank you.
    – Jordan W.
    Dec 18, 2015 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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