Drop down field show/hide options depending on user logged in status

This example is for form id 17. It adds a new option to fields 23 and 24, that only a logged in user can see.

add_filter( 'gform_pre_render_17', 'populate_posts' );
add_filter( 'gform_pre_validation_17', 'populate_posts' );
add_filter( 'gform_pre_submission_filter_17', 'populate_posts' );
add_filter( 'gform_admin_pre_render_17', 'populate_posts' );

function populate_posts( $form ) {

 foreach ( $form['fields'] as &$field ) {

  if ( $field->id == '23' || $field->id == '24') {

    $choices = array();
    $choices[] = array( 'text' => '', 'value' => '' );
    $choices[] = array( 'text' => 'No', 'value' => 'No' );
 $choices[] = array( 'text' => 'Yes', 'value' => 'Yes' );

   if (is_user_logged_in() ) {
 $choices[] = array( 'text' => 'Yes - CONFIRMED', 'value' => 'Yes - CONFIRMED' );
   }

        $field->choices = $choices;
  } 
  }
    return $form;

}
1 Like