Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to upload multiple images as attachments to a post using Gravity Forms and the Gravity Forms + Custom Post Types plugin.

I have a repeatable File Upload field using the Gravity Forms gform_column_input_filter. See this question for how it was achieved.

I then adapted this code from BandonRandon to use the repeatable File Upload field as below:

 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_2']) ) {
     $file_urls = $entry['2']; //great but what is its url?
     $file_array = unserialize($file_urls);

     foreach ( $file_array as $file_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('jpeg' => 'image/jpeg','jpg' => 'image/jpeg', 'gif' => 'image/gif', 'png' => 'image/png') ); //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 tried dumping the $_FILES['input_2'] using the following code at the top of the nifty_add_post_attachements function but no output appears on my screen, the post is still created, and the form ajax loading spinner just hangs.

<pre>
    <?php print_r($_FILES['input_2']); die();?>
</pre>

Any help or pointers in the right direction would be appreciated.

share|improve this question
add comment

1 Answer

Perhaps instead of having a repeatable upload field, use conditional logic to tie a dropdown or radio which asks how many uploads the user needs (1, 2, 3, 4, 5...), and then a series of pre-built upload boxes which show up based on what number was selected. It's not 100% dynamic, but hopefully you have some kind of limit as to how many of these you want in the first place.

I would then store these as meta fields and refer to them. You can map each upload box to a meta field. Then when that post is generated (it is a post right?) you can reference the post_meta.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.