Last active
August 5, 2024 03:45
-
-
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
This file contains hidden or 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
<?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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For similar result, but from a post meta instead of taxonomy...