Skip to content

Instantly share code, notes, and snippets.

@dougkeeling
Last active August 11, 2020 15:42
Show Gist options
  • Save dougkeeling/4a7a9aac1cc9d433e4c27a3da6a25d89 to your computer and use it in GitHub Desktop.
Save dougkeeling/4a7a9aac1cc9d433e4c27a3da6a25d89 to your computer and use it in GitHub Desktop.
[Populate an ACF select field with Gravity Form ID] When all you need is the ID of a Gravity Form in an ACF select field, here's a basic solution that works very well, without having to use a full-blown Gravity Forms field plugin. Simply add a class of "gravity-form-select" to your field and it will be populated automatically. #gravityforms #wor…
function my_acf_load_gravity_forms_select( $field ) {
// This feels slightly hacky, but it works.
//
// If your select field has the wrapper class "gravity-form-select",
// it will be intercepted and any values will be replaced by a list of
// your form IDs and Titles.
//
// You could choose to intercept a select field with a specific name or
// field key, but I prefer this method because you can use it on multiple
// fields with different names across your site.
$field_class = $field['wrapper']['class'];
$gf_class = 'gravity-form-select'; // Add this class to your select field's wrapper.
if(!empty($field_class) && strpos($field_class, $gf_class) !== false):
if ( class_exists( 'GFFormsModel' ) ):
$forms = GFAPI::get_forms();
if ( $forms ):
$field['choices'] = array();
foreach ($forms as $f):
$form_title = $f['title'];
$form_id = $f['id'];
// Add the form ID and Title to the 'choices' array.
// On the front end, only the ID will be returned.
$field['choices'][$form_id] = $form_title;
endforeach;
endif;
endif;
endif;
return $field;
}
add_filter('acf/load_field/type=select', 'my_acf_load_gravity_forms_select');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment