Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Gravity Forms ACF gallery field integration WordPress

  • REFUNDED

I have a form made by Gravity Forms for front end post submissions, everything works fine except the gallery integration, all the inputs save the data into ACF custom fields but there's no native ACF Gallery fields integrations. I found [[LINK href="https://joshuadnelson.com/connect-gravity-forms-file-upload-to-acf-gallery-field/"]]this script[[/LINK]] with the functions to store and upload the multiple images into Wordpress.

I implemented the script as the instructions indicates and images are being upload but not placed into the ACF Gallery Field, instead i get this error.

Answers (3)

2016-01-27

Rempty answers:

Maybe you can check this answer http://www.wpquestions.com/question/showChronoLoggedIn/id/12596


Alvaro Rosado comments:

Hi, thanks. I'll take a look at the thread.


Rempty comments:

Hey Alvaro Rosado
I tested the code from https://joshuadnelson.com/connect-gravity-forms-file-upload-to-acf-gallery-field/

and is working maybe you can send me a PM and i can help you.

2016-01-28

Arnav Joy answers:

is this solved?


Alvaro Rosado comments:

No, i'm still trying to make it work. Will keep trying then i'll post if it was solved.


Alvaro Rosado comments:

No, it's not solved. I've followed the instructions but still no luck.

This is my code with the correct ID's of my site:


/**
* Attach images uploaded through Gravity Form to ACF Gallery Field
*
* @author Joshua David Nelson, [email protected]
* @return void
*/

add_filter( "gform_after_submission_1", 'jdn_set_post_acf_gallery_field', 10, 2 );
function jdn_set_post_acf_gallery_field( $entry, $form ) {
$gf_images_field_id = 16; // the upload field id
$acf_field_id = 'field_547e2025a9af4'; // the acf gallery field id
// get post
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 ) {
if( function_exists( 'jdn_create_image_id' ) )
$image_id = jdn_create_image_id( $value, $post->ID );
if( $image_id ) {
$gallery[] = $image_id;
}
}
}
}
// Update gallery field with array
if( ! empty( $gallery ) ) {
update_field( $acf_field_id, $gallery, $post->ID );
// Updating post
wp_update_post( $post );
}
}


/**
* Other function needed. This is to get image attachment ID
*
* @author Joshua David Nelson, [email protected]
* Source: https://gist.github.com/joshuadavidnelson/164a0a0744f0693d5746
*/

function jdn_create_image_id( $image_url, $parent_post_id = null ) {

if( !isset( $image_url ) )
return false;

// Cache info on the wp uploads dir
$wp_upload_dir = wp_upload_dir();
// get the file path
$path = parse_url( $image_url, PHP_URL_PATH );

// File base name
$file_base_name = basename( $image_url );

// Full path
$home_path = get_home_path();
$home_path = untrailingslashit( $home_path );
$uploaded_file_path = $home_path . $path;

// Check the type of file. We'll use this as the 'post_mime_type'.
$filetype = wp_check_filetype( $file_base_name, null );

// error check
if( !empty( $filetype ) && is_array( $filetype ) ) {
// Create attachment title
$post_title = preg_replace( '/\.[^.]+$/', '', $file_base_name );

// Prepare an array of post data for the attachment.
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $uploaded_file_path ),
'post_mime_type' => $filetype['type'],
'post_title' => esc_attr( $post_title ),
'post_content' => '',
'post_status' => 'inherit'
);

// Set the post parent id if there is one
if( !is_null( $parent_post_id ) )
$attachment['post_parent'] = $parent_post_id;

// Insert the attachment.
$attach_id = wp_insert_attachment( $attachment, $uploaded_file_path );

//Error check
if( !is_wp_error( $attach_id ) ) {
//Generate wp attachment meta data
if( file_exists( ABSPATH . 'wp-admin/includes/image.php') && file_exists( ABSPATH . 'wp-admin/includes/media.php') ) {
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $uploaded_file_path );
wp_update_attachment_metadata( $attach_id, $attach_data );
} // end if file exists check
} // end if error check
return $attach_id;
} // end if $$filetype
} // end function get_image_id


Alvaro Rosado comments:

Kept getting this error:


Warning: Invalid argument supplied for foreach() in /Applications/XAMPP/xamppfiles/htdocs/trabajos-en-proceso/vdtcun/wp/wp-content/plugins/acf-gallery/gallery.php on line 109

2016-01-29

Bob answers:

what is type of the upload field id?