Last active
January 6, 2021 15:00
-
-
Save leepettijohn/28499de95fae967845728ec0be7a43d0 to your computer and use it in GitHub Desktop.
Gravity Forms - Dynamic Checkboxes
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 | |
/*-----------------------------------------------------------------------------------*/ | |
/* Putting Locations in Checkboxes */ | |
/* Helpful article - https://www.gravityhelp.com/documentation/article/dynamically-populating-drop-down-fields/ */ | |
/*-----------------------------------------------------------------------------------*/ | |
$location_form_id = 9; | |
add_filter( 'gform_pre_render_'.$location_form_id, 'populate_posts' ); | |
add_filter( 'gform_pre_validation_'.$location_form_id, 'populate_posts' ); | |
add_filter( 'gform_pre_submission_'.$location_form_id, 'populate_posts' ); | |
add_filter( 'gform_pre_submission_filter_'.$location_form_id, 'populate_posts' ); | |
add_filter( 'gform_admin_pre_render_'.$location_form_id, 'populate_posts' ); | |
function populate_posts( $form ) { | |
foreach ( $form['fields'] as &$field ) { | |
if ( $field->label == 'Location') { | |
$fieldid = $field->id; | |
$terms = get_terms('post_tag',array('hide_empty'=>0)); | |
/* Other qualifiers on https://developer.wordpress.org/reference/functions/get_terms/ */ | |
$choices = array(); | |
$inputs = array(); | |
$idincrease = 1; | |
foreach ( $terms as $post ) { | |
if ( $idincrease % 10 == 0 ) { | |
$idincrease++; | |
} | |
$choices[] = array( 'text' => $post->name, 'value' => $post->name); | |
$inputs[] = array( 'label' => $post->name, 'id' => "{$fieldid}.{$idincrease}" ); | |
$idincrease++; | |
} | |
$field->choices = $choices; | |
$field->inputs = $inputs; | |
} | |
} | |
return $form; | |
} | |
?> |
I set $fieldid in line 16. This pulls from the tags listed in WP. It was basically just helpful for me to understand how to get the checkboxes populated at all.
Sorry...dont see this. Works :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The {$fieldid} isnt declare and get an error. Can you tell me, how to populate with the newest version? after i make a choice, the checkboxes are empty....