Advertisement
chrishajer

Set a default Selected radio button with gform_pre_render

Feb 1st, 2013
776
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.71 KB | None | 0 0
  1. <?php
  2. // http://www.gravityhelp.com/forums/topic/explicitly-set-default-radio-choice-using-hook
  3. // change 29 here to your form ID
  4. add_filter('gform_pre_render_29', 'set_default_dynamically');
  5. function set_default_dynamically($form) {
  6.     foreach( $form['fields'] as &$field ){
  7.         // change 2 here to your field ID
  8.         if( $field['id'] == 2 ){
  9.             foreach($field['choices'] as &$choice) {
  10.                 // I want to set the radio button choice with the value of 31 to selected
  11.                 if($choice['value'] == '31'){
  12.                     $choice['isSelected'] = true;
  13.                 }
  14.                 // unset any default which was set in the form builder
  15.                 else {
  16.                     $choice['isSelected'] = '';
  17.                 }
  18.            }
  19.         }
  20.     }
  21.     // return the modified $form object
  22.     return $form;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement