Skip to content

Instantly share code, notes, and snippets.

@leepettijohn
Last active January 6, 2021 15:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save leepettijohn/28499de95fae967845728ec0be7a43d0 to your computer and use it in GitHub Desktop.
Save leepettijohn/28499de95fae967845728ec0be7a43d0 to your computer and use it in GitHub Desktop.
Gravity Forms - Dynamic Checkboxes
<?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;
}
?>
@rupert-quaderer
Copy link

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....

@leepettijohn
Copy link
Author

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.

@rupert-quaderer
Copy link

Sorry...dont see this. Works :)

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