Created
December 22, 2015 18:47
-
-
Save alenabdula/2b51e31b845119d66071 to your computer and use it in GitHub Desktop.
Gravity form dynamically populate select drop down with taxonomy terms
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 | |
/** | |
* -------------------------------------------------- | |
* 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When adding, form field (select) add class
populate-select
to target only that specific select. This refers to this check: