Skip to content

Learn how to use Gravity Wiz’s Populate Anything perk in conjunction with JetSloth’s Image Choices addon Display Image Choices Entries Using Gravity Perks Populate Anything in Gravity Forms.

Display Image Choices Entries Using Gravity Perks Populate Anything

PHP Snippet

Add the below function to your theme’s function.php file which will connect the Populate Anything add-on with Image Choices.

function gppa_image_choices_from_form_entry( $choice, $field, $object, $objects ) {
	$object_type = rgobj($field, 'gppa-choices-object-type');

	if ( $object_type != "gf_entry" || !function_exists('gf_image_choices') || !gf_image_choices()->field_has_image_choices_enabled($field) ) {
		return $choice;
	}

	$templates = rgobj($field, 'gppa-choices-templates');
	$template = (!empty($templates)) ? rgar($templates, 'imageChoices_image') : "";
	
	if ( empty($template) || strpos($template, 'gf_field_') === FALSE ) {
		return $choice;
	}

	$source_field_id = str_replace('gf_field_', '', $template);
	$source_form_id = rgobj($object, 'form_id');
	$source_value = rgobj($object, $source_field_id);
	
	if ( empty($source_form_id) || !GFAPI::form_id_exists( $source_form_id ) ) {
		return $choice;
	}
	
	$source_field = GFFormsModel::get_field($source_form_id, $source_field_id);

	if ( empty($source_field) || !gf_image_choices()->field_has_image_choices_enabled($source_field) ) {
		return $choice;
	}
	
	foreach( $source_field['choices'] as $source_choice ) {
		if ( rgar($source_choice, 'value') == $source_value ) {
			$choice['imageChoices_image'] = rgar($source_choice, 'imageChoices_image');
			$choice['imageChoices_imageID'] = rgar($source_choice, 'imageChoices_imageID');
			$choice['imageChoices_largeImage'] = rgar($source_choice, 'imageChoices_largeImage');
			break;
		}
	}
	
	return $choice;
}
//add_filter( 'gppa_input_choice', 'gppa_image_choices_from_form_entry', 100, 4);// all forms
//add_filter( 'gppa_input_choice_FORMID', 'gppa_image_choices_from_form_entry', 10, 4);// specific form (ID)
//add_filter( 'gppa_input_choice_FORMID_FIELDID', 'gppa_image_choices_from_form_entry', 10, 4);// specific form and field (ID)

/// By JetSloth.com
/// https://gist.github.com/jetsloth/845149a12ac45bcd3ff03aab36348095

Create the source form (‘Form A’)

Create your first form (Form A) and add a radio or checkbox field to it. Activate Image Choices on the field and add your images as normal. Note you can also use an existing form here that already has image choices entries saved.

Fill out the form a few times with different images and descriptions to create entries that can be used for populating the Image Choices field.

Make sure your Image Choice field has the image and label notification setting enabled so we show the Image Choice selection in entries.

display_image_choices_entries_using_gravity_perks_populate_anything-forma

Create the display form (‘Form B’)

Now, create or choose your display form. This is the form that will display Image Choices entries from form A. Like previously, add a radio or checkbox field to your form, and enable Image Choices.

display_image_choices_entries_using_gravity_perks_populate_anything-formb

Display Image Choices Entries Using Gravity Perks Populate Anything in Gravity Forms

In this tutorial, we explored how to leverage Gravity Wiz’s Populate Anything perk alongside JetSloth’s Image Choices addon within Gravity Forms. We demonstrated how to create a dynamic form that populates image choices from previous form entries and other Image Choices fields.

By following our step-by-step guide, you can set up a seamless workflow where images submitted through an initial form are dynamically available as choices in subsequent forms. This approach is ideal for creating interactive features like dynamic galleries, voting systems, or personalized image selections, enhancing the functionality and user experience of your Gravity Forms.

9,771+ happy customers

Gravity Forms Image Choices

Easily add images as choices for Radio Buttons or Checkboxes fields within Gravity Forms. Includes support for Survey, Quiz, Product and Options.

The Community Loves JetSloth!