Advertisement
chrishajer

Dynamically populate checkboxes

Feb 1st, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.51 KB | None | 0 0
  1. //-------------------------------------------------------
  2. //----------- Dynamically Populate Checkbox  ------------
  3. //-------------------------------------------------------
  4. // Alex Cancado February 1, 2013
  5. // http://www.gravityhelp.com/forums/topic/not-all-the-selected-values-from-checkboxes-are-being-saved-to-the-database#post-137484
  6. // NOTE: update the '221' to the ID of your form
  7. add_filter('gform_pre_render_221', 'populate_checkbox');
  8. add_filter("gform_pre_submission_filter_221", "populate_checkbox");
  9.  
  10. function populate_checkbox($form){
  11.  
  12.     foreach($form['fields'] as &$field){
  13.  
  14.         //NOTE: replace 3 with your checkbox field id
  15.         $field_id = 3;
  16.         if($field['id'] != $field_id)
  17.             continue;
  18.  
  19.         // you can add additional parameters here to alter the posts that are retreieved
  20.         // more info: http://codex.wordpress.org/Template_Tags/get_posts
  21.         $posts = get_posts('numberposts=-1&post_status=publish');
  22.  
  23.         $input_id = 1;
  24.         foreach($posts as $post){
  25.  
  26.             // skipping index that are multiples of 10 (multiples of 10 create problems as the input IDs)
  27.             if($input_id % 10)
  28.                 $input_id++;
  29.  
  30.             $choices[] = array('text' => $post->post_title, 'value' => $post->post_title);
  31.             $inputs[] = array("label" => $post->post_title, "id" => "{$field_id}.{$input_id}");
  32.  
  33.             $input_id++;
  34.         }
  35.  
  36.         $field['choices'] = $choices;
  37.         $field['inputs'] = $inputs;
  38.  
  39.     }
  40.  
  41.     return $form;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement