Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save taricco/2e418ea17e9af154623239a737f0c0d5 to your computer and use it in GitHub Desktop.

Select an option

Save taricco/2e418ea17e9af154623239a737f0c0d5 to your computer and use it in GitHub Desktop.
/** Populate Gravity Forms form hidden field with ACF field "resource_type" data
NOTE: In the Gravity Forms hidden field, enter "resource_type" as the dynamic population parameter. **/
// Populate GF hidden field with parameter name: resource_type
add_filter('gform_field_value_resource_type', function ($value) {
// Bail if ACF isn't loaded
if ( ! function_exists('get_field') ) {
return $value;
}
// Get a reliable post ID even when the form is injected via hooks/Elements
$post_id = get_the_ID();
if ( ! $post_id ) {
$post_id = get_queried_object_id();
}
$acf = get_field('resource_type', $post_id);
// Normalize ACF output to a string
$to_string = function($v) {
// Taxonomy term object(s)
if ( is_object($v) && isset($v->name) ) return (string) $v->name; // use ->slug if you prefer
// ACF select/checkbox set to "Both (Array)"
if ( is_array($v) && (isset($v['label']) || isset($v['value'])) ) {
return (string) ($v['label'] ?? $v['value']);
}
// Mixed arrays (e.g., multiple terms / multiple selects)
if ( is_array($v) ) {
return implode(', ', array_map(function($item){
if (is_object($item) && isset($item->name)) return (string) $item->name;
if (is_array($item)) return (string) ($item['label'] ?? $item['value'] ?? reset($item));
return (string) $item;
}, $v));
}
return (string) $v;
};
$out = $to_string($acf);
// Optional fallback to ACF Options page
if ($out === '' || $out === null) {
$opt = get_field('resource_type', 'option');
if ($opt) $out = $to_string($opt);
}
return $out;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment