Created
July 14, 2021 19:35
-
-
Save jorpdesigns/139d60792f8c1bd995f578426c85025c to your computer and use it in GitHub Desktop.
Snippet to populate Gravity Forms dropdown field with values from custom taxonomy
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 | |
// Replace 8 with your form ID | |
add_filter( 'gform_pre_render_8', 'populate_region_field' ); | |
add_filter( 'gform_pre_validation_8', 'populate_region_field' ); | |
add_filter( 'gform_pre_submission_filter_8', 'populate_region_field' ); | |
add_filter( 'gform_admin_pre_render_8', 'populate_region_field' ); | |
function populate_region_field( $form ) { | |
foreach ( $form['fields'] as &$field ) { | |
if ( strpos( $field->cssClass, 'regions' ) === false ) { // Replace 'regions' with your field class name | |
continue; | |
} | |
$choices = array(); | |
$regions = get_terms( array( | |
'taxonomy' => 'region', // Replace with your custom taxonomy | |
'posts_per_page' => -1, | |
'hide_empty' => false | |
) ); | |
foreach ( $regions as $region ) { | |
$regionName = $region->name; | |
$choices[] = array( 'text' => html_entity_decode($regionName), 'value' => html_entity_decode($regionName), 'isSelected' => false ); | |
} | |
$field->placeholder = 'Select Region(s)'; | |
$field->choices = $choices; | |
} | |
return $form; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment