63 lines
1.7 KiB
PHP
63 lines
1.7 KiB
PHP
<?php
|
|
|
|
// Apply the filter to multiple hooks to ensure choices are available during render, validation, and submission
|
|
add_filter('gform_pre_render', 'populate_taxonomy_terms');
|
|
add_filter('gform_pre_validation', 'populate_taxonomy_terms');
|
|
add_filter('gform_pre_submission_filter', 'populate_taxonomy_terms');
|
|
add_filter('gform_admin_pre_render', 'populate_taxonomy_terms');
|
|
|
|
function populate_taxonomy_terms($form)
|
|
{
|
|
// Specify the form ID and the ID of the select field
|
|
$form_id = 2;
|
|
$field_id = 16;
|
|
|
|
// Check if the current form matches the specified form ID
|
|
if ($form['id'] == $form_id) {
|
|
// Retrieve custom taxonomy terms
|
|
$terms = get_terms(array(
|
|
'taxonomy' => 'etiquettes',
|
|
'hide_empty' => false, // Include empty terms
|
|
));
|
|
|
|
// Find the checkbox field by ID
|
|
foreach ($form['fields'] as &$field) {
|
|
if ($field->id == $field_id) {
|
|
// Prepare choices and inputs array for checkboxes
|
|
$choices = array();
|
|
$inputs = array();
|
|
$index = 1;
|
|
|
|
foreach ($terms as $term) {
|
|
$choices[] = array(
|
|
'text' => $term->name,
|
|
'value' => $term->name, // Use term name as value for display
|
|
'isSelected' => false,
|
|
);
|
|
|
|
// For checkboxes, each choice needs a corresponding input
|
|
$inputs[] = array(
|
|
'id' => $field->id . '.' . $index,
|
|
'label' => $term->name,
|
|
'name' => '',
|
|
);
|
|
|
|
$index++;
|
|
}
|
|
|
|
// Update choices and inputs for the checkbox field
|
|
$field->choices = $choices;
|
|
$field->inputs = $inputs;
|
|
|
|
// Ensure the field is not marked as administrative only
|
|
$field->adminOnly = false;
|
|
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $form;
|
|
}
|