• Hi,
    I’ve created a custom post type, called “PDF”.
    I’ve configured it so only a title and thumbnail are accepted.
    I then added a custom taxonomy, for the PDF file, and added a <input type=”file”> to the Add New Window.

    It looks like it works. But, when a PDF is added, the post is created, but the PDF is not saved.

    Here is my code:

    function pdfa()
    {
      $labels = array(
        'name' => _x('PDFs', 'post type general name'),
        'singular_name' => _x('PDF', 'post type singular name'),
        'add_new' => _x('Add PDF', 'sports article'),
        'add_new_item' => __('Add PDF'),
        'edit_item' => __('Edit PDF'),
        'new_item' => __('Create PDF'),
        'view_item' => __('View PDF'),
        'search_items' => __('Search PDF'),
        'not_found' =>  __('No PDFs found'),
        'not_found_in_trash' => __('No PDFs found in Trash'),
        'parent_item_colon' => ''
      );
      $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => 20,
        'supports' => array('title', 'thumbnail')
      );
      register_post_type('pdfa',$args);
    }
    function admin_init() {
    
    	add_meta_box("pdfMeta", "Attach PDF", "meta_options", "pdfa", "normal", "high");
    
    }
    
    function meta_options() {
    
    	global $post;
    	$custom = get_post_custom($post->ID);
    	$pdf = $custom["pdf_file"][0];
    
    ?>
    
    <label>PDF:</label><input type="file" name="pdf_file" value="<?php echo $pdf; ?>" />
    
    <?php
    
    }
    
    function save_pdf() {
    
    	global $post;
    	update_post_meta($post->ID, "pdfa", $_POST["pdf_file"]);
    
    	}

    Thanks for the help.

    Jeff

Viewing 4 replies - 1 through 4 (of 4 total)
  • sfraser657

    (@sfraser657)

    Hey Jeff,

    This post is seven months old so you probably figured it out or gave up by now…but, anyway, i borrowed your code here so I figured it only fair to explain what you’re missing.

    To upload a file you need to set the form’s encoding to “multipart/form-datat”, like so:

    add_action('post_edit_form_tag', 'add_post_enctype');
    function add_post_enctype() {
        echo ' enctype="multipart/form-data"';
    }

    Your save_pdf() function doesn’t handle the file upload. Here’s what I used to handle this. This will upload the file, as an attachment with the post as parent, and erase the previously uploaded file.

    function save_pdf() {
        if($_FILES["pdf_file"]) {
    	global $post;
            // check filetype is pdf and return if not
            $file_type = $_FILES['pdf_file']['type'];
    	if($file_type != 'application/pdf') return;
            // upload the file
    	$overrides = array( 'test_form' => false);
    	$file = wp_handle_upload($_FILES["pdf_file"],$overrides);
          	if(isset($file['file'])) {
                // Gather data for attachment
                $title = str_replace(".pdf","",$_FILES["pdf_file"]['name']);
                $attachment = array(
                    'post_mime_type' => $file_type,
                    'post_title' => addslashes($title),
                    'post_content' => '',
                    'post_status' => 'inherit',
                    'post_parent' => $post->ID
                );
                //create attachment & update metadata
                $attach_id = wp_insert_attachment( $attachment, $file['file'] );
                // Before we update the post meta, trash any previously uploaded pdfs for this post.
                $existing_pdfa = (int) get_post_meta($post->ID,'pdfa', true);
                if(is_numeric($existing_pdfa)) {
                    wp_delete_attachment($existing_pdfa);
                }
                // Now, update the post meta to associate the new pdf with the post
                update_post_meta($post->ID, "pdfa", $attach_id);
            }
        }
    }

    Probably too late but…hope that helps. ??

    Thanks for the code, sfraser657! I’ve got this working, but I’m not able to see that a file is already attached when I go to edit the pdf post type. What code could I use to display what has already been added?
    Will this work with multiple attachments of different types (pdf, doc, word)?

    I’ve got the same question as @jfelton, this code doesn’t seem to generate anything:
    $pdf = $custom["pdf_file"][0];

    Ah, I had a look at the database and it fell into place:
    wp_get_attachment_url(get_post_meta($post->ID,'pdfa',true))

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom post types and file upload taxonomies’ is closed to new replies.