Skip to content
Skip to the article
In WordPress: 10 articles
WordPress

Populate Gravity Forms image choices from an ACF image field

Use an ACF image field on each post as the picture for Image Choices when Populate Anything fills a Gravity Forms choice field with posts.

Updated
Applies to
  • WordPress 6.x
  • Gravity Forms 2.8+
  • GP Populate Anything 2.x
  • JetSloth Image Choices 1.4+
  • ACF 6.x
Tags
  • gravity-forms
  • acf
  • populate-anything
  • php
Reading time
3 min

GP Populate Anything can fill a Radio or Checkbox field with posts, and JetSloth's Image Choices add-on shows a picture for each choice. Populate Anything's Image template can map a featured image URL directly, but an ACF image field stores an attachment ID, which Image Choices can't display as-is. This snippet looks up each post's ACF image and hands Image Choices the right ID and thumbnail URL.

Requirements

  • A Radio or Checkbox field with Use Image Choices enabled.

  • Populate Anything set to populate that field's choices from Post objects (for example a my_cpt post type).

  • An ACF image field on those posts. The examples use the field name image.

The Snippet

Put this in a site plugin or a child theme's functions.php. Replace <FORM_ID> and <FIELD_ID> with the form and field you're populating:

<?php
/**
 * Image Choices: use each populated post's ACF "image" field as the choice image.
 */
add_filter( 'gppa_input_choices_<FORM_ID>_<FIELD_ID>', function ( $choices, $field, $objects ) {
	if ( empty( $field->imageChoices_enableImages ) ) {
		return $choices;
	}

	foreach ( $choices as $i => &$choice ) {
		if ( ! isset( $objects[ $i ]->ID ) ) {
			continue;
		}

		// false = raw value, so this is the attachment ID whatever the field's return format.
		$image_id = (int) get_field( 'image', $objects[ $i ]->ID, false );
		if ( ! $image_id ) {
			continue;
		}

		$src = wp_get_attachment_image_src( $image_id, 'thumbnail' );
		if ( $src ) {
			$choice['imageChoices_imageID'] = $image_id;
			$choice['imageChoices_image']   = $src[0];
		}
	}

	return $choices;
}, 100, 3 );

How it works:

  • gppa_input_choices runs after Populate Anything builds the choices. $objects holds the posts the choices came from. As in JetSloth's own example, the snippet relies on both arrays being in the same order, so $objects[ $i ] is the post behind $choices[ $i ].

  • The _<FORM_ID>_<FIELD_ID> suffix limits the filter to one field. Use the bare gppa_input_choices hook to apply it to every Image Choices field that Populate Anything fills.

  • Image Choices reads imageChoices_image (the URL to show) and imageChoices_imageID (the attachment, used for the lightbox and larger sizes). Set imageChoices_largeImage as well if you want a specific lightbox image.

  • get_field( ..., false ) returns the stored attachment ID even if the ACF field returns an array or URL, so the snippet doesn't break when someone changes the field's return format.

Note

The index matching assumes nothing has added or removed choices before priority 100. If another filter prepends a static choice (like "Select one"), $objects and $choices no longer line up. In that case, give this filter a smaller priority number than the one that adds the static choice, so it runs first.

Thumbnail Size

thumbnail is WordPress's 150×150 crop by default. To use another size, register it in the theme and pass its name:

add_action( 'after_setup_theme', function () {
	add_image_size( 'choice-thumb', 300, 300, true );
} );

Then use wp_get_attachment_image_src( $image_id, 'choice-thumb' ). Regenerate thumbnails for existing images with wp media regenerate --only-missing.

This version is adapted from JetSloth's published featured-image example (see references). It reads the ACF image instead, always moves on to the next post even when one has no image, and uses core image sizes instead of a third-party resizer.