First value as default in populated radio buttons field

I managed to pass the values of a ACF checkbox field (colors) to a GF radio button field, showing the colors blue (first value) and red (second value). Does anyone know how to set the first value (e.g. blue) as default choice in the radio button field? My code is below. So how can I preselect the first value of the $radiobuttons array?

add_filter( 'gform_pre_render_1', 'populate_radio_colors' );
add_filter( 'gform_pre_validation_1', 'populate_radio_colors' );
add_filter( 'gform_admin_pre_render_1', 'populate_radio_colors' );
add_filter( 'gform_pre_submission_filter_1', 'populate_radio_colors' );

	function populate_radio_colors( $form ) {
		if ( $form['id'] != 1 ) {
			return $form;
		}
$post_id = get_query_var('training')
$colors = get_field( 'colors' , $post_id );

    // declare the array
    $radiobuttons = array();


// set array
    foreach ($colors as $color) {
      $radiobuttons[] = array(
        'value' => $color,
        'text' => $color,
      );
    }

    //Add repeater values as radio button choices
    foreach ( $form['fields'] as &$field ) {
      if ( $field->id == 49 ) {
        $field->choices = $radiobuttons;
      }
    }

    return $form;
    wp_reset_postdata();
  }	

Within your $colors loop where you’re defining the $radiobuttons array, you would want to add a check for the value you want to make the first choice (if you’re doing it for a specific color) and add ‘isSelected’ => true

I’m not a PHP developer (I just play one on support), but that is the option for making a choice selected from this article:

Under the section ‘Creating an Array of Choices’.

Hope that helps!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.