Last active
September 17, 2024 20:43
-
-
Save saltnpixels/1056f39b97c1d19ee18f8e30fbdada6d to your computer and use it in GitHub Desktop.
gravity form upload file to media library and use attachment ID in custom field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'gform_after_create_post', 'gf_add_to_media_library', 10, 3 ); | |
/** | |
* Save file upload fields under custom post field to the library | |
* | |
* @param $post_id The post identifier | |
* @param $entry The entry | |
* @param $form The form | |
*/ | |
function gf_add_to_media_library ( $post_id, $entry, $form ) { | |
foreach($form['fields'] as $field){ | |
//get media upload dir | |
$uploads = wp_upload_dir(); | |
$uploads_dir = $uploads['path']; | |
$uploads_url = $uploads['url']; | |
//if its a custom field with input type file upload. | |
if( $field['type'] == 'post_custom_field' && $field['inputType'] == 'fileupload'){ | |
$entry_id = $field['id']; | |
$files = rgar ( $entry, $entry_id ); | |
$custom_field = $field['postCustomFieldName']; //custom field key | |
//if file field is not empty or not [] | |
if ( $files !== '' && $files !== "[]"){ | |
$patterns = ['[', ']', '"']; //get rid of crap | |
$file_entry = str_replace($patterns, '', $files); | |
$files = explode ( ',', $file_entry ); | |
foreach ($files as $file) { | |
//each file is a url | |
//get the filename from end of url in match[1] | |
$filename = pathinfo($file, PATHINFO_FILENAME); | |
//add to media library | |
//WordPress API for image uploads. | |
include_once( ABSPATH . 'wp-admin/includes/image.php' ); | |
include_once( ABSPATH . 'wp-admin/includes/file.php' ); | |
include_once( ABSPATH . 'wp-admin/includes/media.php' ); | |
$new_url = stripslashes($file); | |
$result = media_sideload_image( $new_url, $post_id, $filename, 'src'); | |
//saving the image to field or thumbnail | |
if( strpos($field['cssClass'], 'thumb') === false ){ | |
$attachment_ids[] = (int) get_attachment_id_from_src($result); | |
} | |
else{ | |
set_post_thumbnail($post_id, (int) get_attachment_id_from_src($result) ); | |
} | |
} //end foreach file | |
if ( isset( $attachment_ids ) ){ | |
update_post_meta ($post_id, $custom_field, $attachment_ids); | |
} | |
} //end if files not empty | |
} //end if custom field of uploadfile | |
} | |
} //end for each form field | |
function get_attachment_id_from_src($image_src) { | |
global $wpdb; | |
$query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$image_src'"; | |
$id = $wpdb->get_var($query); | |
return $id; | |
} |
I copied the code to my functions.php file and changed the "postCustomFieldName" to my custom field name "test_image_upload". but it is not working. when I upload an image to the field, there is no image uploaded to the media library. Any one knows why?
I did this too, but turns out postCustomFieldName needs to stay unchanged. It's the field key that is connected to your custom field name.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here are a couple of notes for any folks finding this!
gform_after_create_post
doesn't seem to work with a custom post type. I figured out that usinggform_advancedpostcreation_post_after_creation
does the trick!Also, it seems to only work with images and only multiple files at a time. I made some tweaks and integrated a file uploader that works with more file types here: https://gist.github.com/RadGH/966f8c756c5e142a5f489e86e751eacb
Here's my end result for getting pdf/images/other files working for single file upload: