Skip to content

Instantly share code, notes, and snippets.

@dompl
Forked from eugenoprea/functions.php
Created December 8, 2022 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dompl/9c74cb0f8cd9d97803f2d97a4cc45fd8 to your computer and use it in GitHub Desktop.
Save dompl/9c74cb0f8cd9d97803f2d97a4cc45fd8 to your computer and use it in GitHub Desktop.
Gravity Forms - Checkbox Dynamic Population
// When true, the form will be saved to DB after dynamic population
define('EO_SAVE_FORM_ON_PRE_RENDER', true);
// Adds a filter to form id 7. Replace 26 with your actual form id
add_filter('gform_pre_render_7', 'eo_populate_checkbox');
add_filter('gform_admin_pre_render_7', 'eo_populate_checkbox');
function eo_populate_checkbox($form) {
if (EO_SAVE_FORM_ON_PRE_RENDER)
$the_form = RGFormsModel::get_form_meta($form['id']);
else
$the_form = &$form;
// Creating choices
$choices = array(
array('text' => 'Choice 1', 'value' => 'choice1'),
array('text' => 'Choice 2', 'value' => 'choice2'),
array('text' => 'Choice 3', 'value' => 'choice3'),
);
$inputs = array(
array('label' => 'Choice 1', 'id' => '2.1'), // replace 2 in 2.1 with your field id
array('label' => 'Choice 2', 'id' => '2.2'), // replace 2 in 2.2 with your field id
array('label' => 'Choice 3', 'id' => '2.3'), // replace 2 in 2.3 with your field id
);
// Adding items to field id 2. Replace 2 with your actual field id.
// You can get the field id by looking at the input name in the markup.
foreach ($the_form['fields'] as &$field) {
// replace 2 with your checkbox field id
if ($field['id'] == 2) {
$field['choices'] = $choices;
$field['inputs'] = $inputs;
}
}
// Save form to database, if constant is set to true
if (EO_SAVE_FORM_ON_PRE_RENDER)
RGFormsModel::update_form_meta($form['id'], $the_form);
return $the_form;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment