0

Using gravity forms as a front end file up-loader, users can upload gallery images via a form which then saves to a ACF ( advanced custom forms ) gallery field.

It works to a point, the form uploads the images and they are stored in gravity form entry (you can view the images just fine).

The second part of the code below attempts to move the image from the gravity form attachment into the media library as a post attachment

This is where it fails, leaving the image file name but a broken image ( 0kb in size ).

The original code can be found here, but I'm at a loss as to where the problem is : https://joshuadnelson.com/connect-gravity-forms-file-upload-to-acf-gallery-field/

/**
 * Attach images uploaded through Gravity Form to ACF Gallery Field
 *
 * @return void
 */
$gravity_form_id = 10; // gravity form id, or replace {$gravity_form_id} below with this number
add_filter( "gform_after_submission_{$gravity_form_id}", 'jdn_set_post_acf_gallery_field', 10, 2 );
function jdn_set_post_acf_gallery_field( $entry, $form ) {

 $gf_images_field_id = 69; // the upload field id
 $acf_field_id = 'field_59e260b6e28fd'; // the acf gallery field id

 // get post, if there isn't one, bail
 if( isset( $entry['post_id'] ) ) {
 $post = get_post( $entry['post_id'] );
 if( is_null( $post ) )
 return;
 } else {
 return;
 }

 // Clean up images upload and create array for gallery field
 if( isset( $entry[ $gf_images_field_id ] ) ) {
 $images = stripslashes( $entry[ $gf_images_field_id ] );
 $images = json_decode( $images, true );
 if( !empty( $images ) && is_array( $images ) ) {
 $gallery = array();
 foreach( $images as $key => $value ) {
 // NOTE: this is the other function you need: https://gist.github.com/joshuadavidnelson/164a0a0744f0693d5746
 if( ! class_exists( 'JDN_Create_Media_File' ) )
 break;

 // Create the media library attachment and store the attachment id in the gallery array
 $create_image = new JDN_Create_Media_File( $value, $post->ID );
 $image_id = $create_image->attachment_id;
 if( absint( $image_id ) ) {
 $gallery[] = $image_id;
 }
 }
 }
 }

 // Update gallery field with array
 if( ! empty( $gallery ) ) {
 update_field( $acf_field_id, $gallery, $post->ID );
 }
}

__

  <?php
    class JDN_Create_Media_File {

     var $post_id;
     var $image_url;
     var $wp_upload_url;
     var $attachment_id;

     /**
     * Setup the class variables
 */
 public function __construct( $image_url, $post_id = 0 ) {

 // Setup class variables
 $this->image_url = esc_url( $image_url );
 $this->post_id = absint( $post_id );
 $this->wp_upload_url = $this->get_wp_upload_url();
 $this->attachment_id = $this->attachment_id ?: false;

 return $this->create_image_id();

 }

 /**
 * Set the upload directory
 */
 private function get_wp_upload_url() {
 $wp_upload_dir = wp_upload_dir();
 return isset( $wp_upload_dir['url'] ) ? $wp_upload_dir['url'] : false;
 }

 /**
 * Create the image and return the new media upload id.
 *
 * @see https://gist.github.com/hissy/7352933
 *
 * @see http://codex.wordpress.org/Function_Reference/wp_insert_attachment#Example
 */
 public function create_image_id() {

 if( $this->attachment_id )
 return $this->attachment_id;

 if( empty( $this->image_url ) || empty( $this->wp_upload_url ) )
 return false;

 $filename = basename( $this->image_url );

 $upload_file = wp_upload_bits( $filename, null, file_get_contents( $this->image_url ) );

 if ( ! $upload_file['error'] ) {
 $wp_filetype = wp_check_filetype( $filename, null );
 $attachment = array(
 'post_mime_type' => $wp_filetype['type'],
 'post_parent' => $this->post_id,
 'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
 'post_content' => '',
 'post_status' => 'inherit'
 );
 $attachment_id = wp_insert_attachment( $attachment, $upload_file['file'], $this->post_id );

 if( ! is_wp_error( $attachment_id ) ) {

 require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
 require_once( ABSPATH . 'wp-admin/includes/media.php' );

 $attachment_data = wp_generate_attachment_metadata( $attachment_id, $upload_file['file'] );
 wp_update_attachment_metadata( $attachment_id, $attachment_data );

 $this->attachment_id = $attachment_id;

 return $attachment_id;
 }
 }

 return false;

 } // end function get_image_id
}

closed as off-topic by Mark Kaplun, bueltge, Jack Johansson, Pat J, Christine Cooper Oct 22 '17 at 10:38

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

1

An even easier option is to use my Gravity Forms Media Library plugin. Here's the meat and potatoes of the functionality:

public function maybe_upload_to_media_library( $entry, $form ) {

    $has_change = false;

    foreach( $form['fields'] as $field ) {

        if( ! $this->is_applicable_field( $field ) ) {
            continue;
        }

        $value = $entry[ $field->id ];

        if( $field->multipleFiles ) {
            $value = json_decode( $value );
        }

        if( empty( $value ) ) {
            continue;
        }

        $has_change = true;
        $ids        = $this->upload_to_media_library( $value, $field, $entry );
        $new_value  = array();

        if( is_wp_error( $ids ) ) {
            continue;
        }

        foreach( $ids as $id ) {
            if( ! is_wp_error( $id ) ) {
                $new_value[] = wp_get_attachment_url( $id );
            }
        }

        if( $field->multipleFiles ) {
            $new_value = json_encode( $new_value );
        } else {
            $new_value = $new_value[0];
            $ids       = $ids[0];
        }

        $entry[ $field->id ] = $new_value;

        $this->update_file_ids( $entry['id'], $field->id, $ids );

    }

    if( $has_change ) {
        GFAPI::update_entry( $entry );
    }

    return $entry;
}

But... it also handles a ton of other use-cases you may not be considering such as updating the entry, multi-file uploads, and auto-integration with ACF various image-based custom field types.

0

I had a similiar problem and found a solution for my specific problem using 'media_handle_upload' (https://codex.wordpress.org/Function_Reference/media_handle_upload).

First - Make sure your form has the attribute 'enctype='multipart/form-data'.

Second - Be sure that your 'input=file' has no value attribute.

Third - Use the 'media_handle_upload' and pass the name of your 'input=file'.

Forth - Check if there was an error with 'is_wp_error', for example.

Fifth - Update the user meta using the name of the field you want to update (in my case, is the same as the 'name' of the 'input=file'.

My question and complete answer are here: ACF Upload Image in front-end with custom form

Hope my code can help you with your question... It probably can, you just have to adapt it.

  • Thanks Marcelo, I had read your thread and a couple of others. I'm not sure it applies in this case because gravity forms upload the images just fine, its the part where the above code should then move the images from the gravity forms and attach them to a post which seems to fail. – Randomer11 Oct 17 '17 at 15:05
  • And that was my problem. I had empty images showing in the library, just like you. That's why I think the same solution can be aplied. Try changing the 'wp_upload_bits' for the 'media_handle_upload'. – Marcelo Henriques Cortez Oct 17 '17 at 15:29
  • Oh I see, Ok I will give it a shot after dinner and report back :) – Randomer11 Oct 17 '17 at 15:34
  • No unfortunately that didnt work, Just crashes the form on submission. – Randomer11 Oct 17 '17 at 16:07

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