admin管理员组

文章数量:1336632

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');

本文标签: Custom Metabox Info Not Saving