Okay so like may others I love Gravity Forms. Simple, elegant forms for WordPress. However, I recently ran into a snag when trying to use the awesome Custom Post Type plugin to allow my client to upload a PDF then have that be saved as an attachment to the post. Here is my solution in hopes it helps you. I’m open to suggestions on better ways to do this.
add_filter("gform_after_submission", "nifty_add_post_attachements", 10, 3); function nifty_add_post_attachements($entry) { //you'll need this later, TRUST ME. if(!function_exists('wp_generate_attachment_metadata')){require_once(ABSPATH . 'wp-admin/includes/image.php');} //do we even have a file? if (isset($_FILES['input_4'])) { $file_url = $entry['4']; //great but what is its url? $upload_dir = wp_upload_dir(); //where do you want to put it? $file_data = file_get_contents($file_url); //show me what you're made of $filename = basename($file_url); //so cute but what's its name? if(wp_mkdir_p($upload_dir['path'])) //can we put it there? $file = $upload_dir['path'] . '/' . $filename; //yes great else //or no, okay fine let's try somewhere else $file = $upload_dir['basedir'] . '/' . $filename; //get the whole location file_put_contents($file, $file_data); // tada home at last $wp_filetype = wp_check_filetype($filename, array('pdf' => 'application/pdf','pdf' => 'application/x-pdf') ); //is it the right type of of file? $attachment = array( //set up the attachment 'post_mime_type' => $wp_filetype['type'], 'post_title' => sanitize_file_name($filename), 'post_content' => '', 'post_status' => 'inherit' ); $attach_id = wp_insert_attachment( $attachment, $file, $entry['post_id'] ); //insert attachment $attach_data = wp_generate_attachment_metadata( $attach_id, $file ); //asign the meta wp_update_attachment_metadata( $attach_id, $attach_data ); //update the post //remove the entry //(to keep the entry comment or delete these line) // IMPORTANT See: http://pastebin.com/quvsvGHJ for the function if(!function_exists('sedc_gform_remove_entries')){ die('next time maybe you should read the comments');} else{ nifty_gform_remove_entries($entry, $form );} } }
I have included a function that removes the entry and original file upload. That function can be found here. There is also a simpler version that doesn’t give a damn about the file upload that can be found here. Both are original from the GF support forms but have been slightly modified for v 1.6. I should also point out that some of the upload code came from here.
Hope you find this useful
Hi simple question, does this work with all forms I don’t see it targeting a specific form, less I’m missing something. Also, how do I place a link to the file from within the loop? Thanks!
Looks like this no longer works. I am using that hook and $_FILES is empty. The field with type fileupload has a comma delimited slashed string. I am doing:
`$files = str_replace(‘”‘, ”, stripslashes($_entry[$field->ID]]));
$files = explode(‘,’, $files);`
to get an array of uploaded files. They are uploaded to the gravity form uploads in an encoded folder.
I can then break apart each file to get the filename and path.
I’m getting $field by looping through $form[‘fields’] and processing each field with $entry.
Actually, I just realized i just need to:
$files = json_decode($entry[$field->id]);
can you help me with the new full code i can’t figure out why it isn’t working
Hi, I’m a bit new to “Custom Post Type”. Is there a step by step instruction on how to use your code to include uploaded file (PDF) as post attachment?