0

I have a custom metabox on a custom post type, and the input data disappears after I save/update the post. I've read through a bunch of similar questions but can't find an answer that fixes it for me. Does anyone see what I'm doing wrong here?

Thanks!

function whitepaper_pdf_info() {
    add_meta_box( 
        'whitepaper_pdf_info',
        __( 'Whitepaper Upload & Details', 'customwp' ),
        'whitepaper_pdf_info_content',
        'whitepaper',
        'normal',
        'high'
    );
}
add_action( 'add_meta_boxes', 'whitepaper_pdf_info' );

function whitepaper_pdf_info_content( $post ) {
    wp_nonce_field( plugin_basename( __FILE__ ), 'whitepaper_pdf_info_content_nonce' );

    $html = '<p class="description">Upload Whitepaper PDF</p>';
    $html .= '<input type="file" id="whitepaperfile" name="whitepaperfile" accept="application/pdf" required />';

    $html .= '<p class="description">Description of Whitepaper</p>';
    $html .= '<textarea id="whitepaperdescription" name="whitepaperdescription" placeholder="Description of Whitepaper" style="width: 100%;" rows="10"></textarea>';

    echo $html;
}

function whitepaper_pdf_info_save($id) {

    // Security Checks
    if(!wp_verify_nonce($_POST['whitepaper_pdf_info_content_nonce'], plugin_basename(__FILE__))) {
      return $id;
    } 

    if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
      return $id;
    } 

    if ( 'whitepaper' == $_POST['post_type'] ) { 
        if ( !current_user_can( 'edit_page', $id )) { 
            return $id; 
        }
    }

    // Handle the Input Data
    if(!empty($_FILES['whitepaperfile']['name'])) {         
        // Setup the array of supported file types (PDFs)
        $supported_types = array('application/pdf');         
        // Get the file type of the upload
        $arr_file_type = wp_check_filetype(basename($_FILES['whitepaperfile']['name']));
        $uploaded_type = $arr_file_type['type'];         
        // Check if the type is supported. If not, throw an error.
        if(in_array($uploaded_type, $supported_types)) { 
            $upload = wp_upload_bits($_FILES['whitepaperfile']['name'], null, file_get_contents($_FILES['whitepaperfile']['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, 'whitepaperfile', $upload);
                update_post_meta($id, 'whitepaperfile', $upload);     
            }  
        } else {
            wp_die("The file type that you've uploaded is not a PDF.");
        }         
    }

    $description = $_POST['whitepaperdescription'];
    update_post_meta( $id, 'whitepaperdescription', $description );

} 
add_action('save_post', 'whitepaper_pdf_info_save');
  • Welcome to WPSE! Did you check if the data is actually saved to database? There are a mainly 2 possibilities. 1) it did not saved. 2) it did save but it did not fetch correctly. So your direction to debug this 2 things could probably resolve it. – simongcc May 21 at 3:17
  • @simongcc Thanks! Looks like it's not saving in the db. I don't see anything in wp_postmeta connected to the post (but the post itself shows up correctly in wp_posts). – trose1189 May 21 at 19:08
  • You may use var_dump and exit function in your save logic to see which part is missed during save. – simongcc May 21 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.