Skip to content

Instantly share code, notes, and snippets.

@jetsloth
Last active August 5, 2024 03:45
Show Gist options
  • Save jetsloth/f7c373664c2985e796c98450720eedea to your computer and use it in GitHub Desktop.
Save jetsloth/f7c373664c2985e796c98450720eedea to your computer and use it in GitHub Desktop.
An example for Image Choices with Gravity Perks Populate Anything, where the object is a taxonomy and the ACF field returns either an image ID or object
<?php
function gppa_image_choices_from_term_acf_field( $choice, $field, $object, $objects ) {
$object_type = rgobj($field, 'gppa-choices-object-type');
if ( !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, "meta_") === FALSE ) {
return $choice;
}
$taxonomy = "category";
$term_id = rgobj($object, 'term_id');
$meta_name = str_replace("meta_", "", $template);
// if the ACF returns the ID...
$image_id = get_field($meta_name, "{$taxonomy}_{$term_id}");
$image_url = wp_get_attachment_image_src($image_id, "medium");// change to the size you want
// if the ACF returns the array...
//$image_object = get_field($meta_name, "{$taxonomy}_{$term_id}");
//$image_url = (!empty($image_object)) ? $image_object['sizes']['medium'];// change to the size you want
$choice['imageChoices_image'] = $image_url;
$choice['imageChoices_largeImage'] = $image_url;// this if for the lightbox feature, if using. Can be a larger image
return $choice;
}
add_filter( 'gppa_input_choice_FORMID_FIELDID', 'gppa_image_choices_from_term_acf_field', 100, 4);
@jetsloth
Copy link
Author

jetsloth commented Aug 5, 2024

For similar result, but from a post meta instead of taxonomy...

<?php
function gppa_image_choices_from_post_acf_field( $choice, $field, $object, $objects ) {
	$object_type = rgobj($field, 'gppa-choices-object-type');

	if ( !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, "meta_") === FALSE ) {
		return $choice;
	}

	$post_id = rgobj($object, 'ID');
	$meta_name = str_replace("meta_", "", $template);

	// if the ACF returns the ID...
	$image_id = get_field($meta_name, $post_id);
	$image_url = wp_get_attachment_image_src($image_id, "medium");// change to the size you want

	// if the ACF returns the array...
	//$image_object = get_field($meta_name, $post_id);
	//$image_url = (!empty($image_object)) ? $image_object['sizes']['medium'];// change to the size you want

	$choice['imageChoices_image'] = $image_url;
	$choice['imageChoices_largeImage'] = $image_url;// this if for the lightbox feature, if using. Can be a larger image
	
	return $choice;
}
add_filter( 'gppa_input_choice_FORMID_FIELDID', 'gppa_image_choices_from_post_acf_field', 100, 4);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment