Last active
November 19, 2019 19:55
-
-
Save bcreeves/ce79d507a28bd6e701c9152656057c4d to your computer and use it in GitHub Desktop.
gform filter for dynamically populating checkboxes and selects
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
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