Skip to content

Instantly share code, notes, and snippets.

@bcreeves
Last active November 19, 2019 19:55
Show Gist options
  • Save bcreeves/ce79d507a28bd6e701c9152656057c4d to your computer and use it in GitHub Desktop.
Save bcreeves/ce79d507a28bd6e701c9152656057c4d to your computer and use it in GitHub Desktop.
gform filter for dynamically populating checkboxes and selects
add_filter('gform_pre_render', 'cr_add_locations_to_select_hr');
add_filter('gform_pre_validation', 'cr_add_locations_to_select_hr');
add_filter('gform_pre_submission_filter', 'cr_add_locations_to_select_hr');
add_filter('gform_admin_pre_render', 'cr_add_locations_to_select_hr');
function cr_add_locations_to_select_hr($form)
{
foreach ($form['fields'] as &$field) {
if (strpos($field->cssClass, 'edit-post-populate-locations-hr') === false) {
continue;
}
$choices = [];
$locations = list_locations_hr();
if (strpos($field->cssClass, 'no-id-values') === false) {
foreach ($locations as $location) {
$choices[] = ['text' => $location->post_title, 'value' => $location->ID];
}
} else {
foreach ($locations as $location) {
$choices[] = ['text' => $location->post_title, 'value' => $location->post_title];
}
}
$field->placeholder = null;
$field->choices = $choices;
}
return $form;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment