Forum Replies Created

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hey,

    The mistake in the code is pretty obvious, if anyone is still interested in this. You haven’t defined $blog_id; just add “global $blog_id;” to the beginning of your function and it works great.

    Peace,
    Steve

    sfraser657

    (@sfraser657)

    at the dashboard – top right – did you check to show custom fields?

    No, not at the dashboard, on the edit posts page, check under “Screen Options” on the top right

    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. ??

Viewing 3 replies - 1 through 3 (of 3 total)