Skip to content

Instantly share code, notes, and snippets.

@alenabdula
Created December 22, 2015 18:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alenabdula/2b51e31b845119d66071 to your computer and use it in GitHub Desktop.
Save alenabdula/2b51e31b845119d66071 to your computer and use it in GitHub Desktop.
Gravity form dynamically populate select drop down with taxonomy terms
<?php
/**
* --------------------------------------------------
* Gravity Form dynamically populate drop-down
* select for Questions form ID 15
* 'gform_pre_render_14' - _14 represents form ID to filter
* --------------------------------------------------
*/
add_filter( 'gform_pre_render_14', 'pre_populate_gravity_select_for_MY_TAX' );
add_filter( 'gform_pre_validation_14', 'pre_populate_gravity_select_for_MY_TAX' );
add_filter( 'gform_pre_submission_filter_14', 'pre_populate_gravity_select_for_MY_TAX' );
add_filter( 'gform_admin_pre_render_14', 'pre_populate_gravity_select_for_MY_TAX' );
function pre_populate_gravity_select_for_MY_TAX($form) {
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-select' ) === false ) {
continue;
}
$terms = get_terms(array('MY_TAXONOMY'));
$choices = array();
foreach ( $terms as $term ) {
$choices[] = array( 'text' => $term->name, 'value' => $term->name );
}
$field->placeholder = 'Select Option';
$field->choices = $choices;
}
return $form;
}
@alenabdula
Copy link
Author

When adding, form field (select) add class populate-select to target only that specific select. This refers to this check:

strpos( $field->cssClass, 'populate-select' ) === false

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