Auto Select Only Option

Experimental Snippet 🧪

The Gravity Forms Chained Selects field requires you to manually select a value in each Drop Down even if there is only a single option available in that Drop Down. This snippet will automatically selected an option when it is the only option available.

Code

Filename: gfcs-auto-chained-select.php

<?php
/**
 * Gravity Wiz // Gravity Forms Chained Selects // Auto Select Only Option
 * https://gravitywiz.com/
 *
 * Experimental Snippet 🧪
 *
 * The Gravity Forms Chained Selects field requires you to manually select a value in each Drop Down
 * even if there is only a single option available in that Drop Down. This snippet will automatically
 * selected an option when it is the only option available.
 */
add_filter( 'gform_chained_selects_input_choices', function( $choices ) {
	$choices = gfcs_auto_select_only_choice( $choices );
	return $choices;
} );

function gfcs_auto_select_only_choice( $choices ) {

	$choices[0]['isSelected'] = $choices[0]['isSelected'] || count( $choices ) <= 1;

	if ( ! empty( $choices['choices'] ) ) {
		$choices['choices'] = gfcs_auto_select_only_choice( $choices['choices'] );
	}

	return $choices;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.