Use PHP to pre-populate Image Hopper fields with existing images when the form renders. This is useful for allowing users to edit or replace previously uploaded images.
Important: When using PHP pre-population, unless the image is edited or removed by the user, Gravity Forms won’t reprocess the image on form submission. This mirrors the type: 'local' option in the JavaScript version. If you need the image to be processed with every form submission, use the JavaScript approach with type: 'input' instead.
Pre-populate Image Hopper fields via the gform_pre_render action:
add_action( 'gform_pre_render', function( $form ) {
/* Only run on form ID 2 */
if ( (int) $form['id'] !== 2 ) {
return $form;
}
if ( ! isset( GFFormsModel::$uploaded_files[ $form['id'] ] ) ) {
GFFormsModel::$uploaded_files[ $form['id'] ] = [];
}
/* Pre-populate Image Hopper field ID#1 with images */
GFFormsModel::$uploaded_files[ $form['id'] ]['input_1'] = [
[
'temp_filename' => '',
'uploaded_filename' => 'https://example.com/path/to/image1.jpg',
],
[
'temp_filename' => '',
'uploaded_filename' => 'https://example.com/path/to/image2.jpg',
],
[
'temp_filename' => '',
'uploaded_filename' => 'https://example.com/path/to/image3.jpg',
],
];
/* Pre-populate Image Hopper field ID#2 with images */
GFFormsModel::$uploaded_files[ $form['id'] ]['input_2'] = [
[
'temp_filename' => '',
'uploaded_filename' => 'https://example.com/path/to/image1.jpg',
],
];
return $form;
} );
Note: Replace form ID 2 with your actual form ID, input_1 and input_2 with your field IDs (prefixed with input_), and the URLs with your actual image URLs.
Related
- PHP Actions/Filters — the filter hub
- Pre-populate an image on form load — seed images using JavaScript instead of PHP