Skip to content

Instantly share code, notes, and snippets.

@kjbrum
Last active March 22, 2021 23:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kjbrum/667548e1ccb8c29055be to your computer and use it in GitHub Desktop.
Save kjbrum/667548e1ccb8c29055be to your computer and use it in GitHub Desktop.
Add a blank option to a Gravity Forms select element.
<?php
/**
* Add a blank option to a Gravity Forms dropdown
*
* @param object $form The Gravity Form
* @return object $form The modified Gravity Form
*/
function wp_gravity_forms_add_empty_dropdown_option( $form ) {
// Select the correct form, then set the id of the field(s) we need to change
if( $form['id'] == 1 ) {
$field_id = array(1,2);
} else {
return $form;
}
// Loop through the form fields
foreach( $form['fields'] as &$field ) {
// Check for the field we need
foreach( $field_id as $id ) {
if( $field->id == $id ) {
// Add blank first element
$items = array(
'text' => '',
'value' => '',
'isSelected' => true,
'price' => ''
);
// Add to the top of the array
array_unshift( $field->choices, $items );
}
}
}
return $form;
}
add_filter( 'gform_pre_render', 'wp_gravity_forms_add_empty_dropdown_option' );
add_filter( 'gform_pre_submission_filter', 'wp_gravity_forms_add_empty_dropdown_option' );
// add_filter( 'gform_pre_validation', 'wp_gravity_forms_add_empty_dropdown_option' ); // Use if ajax isn't enabled
// add_filter( 'gform_admin_pre_render', 'wp_gravity_forms_add_empty_dropdown_option' ); // Use to show blank choice in admin area
@972creative
Copy link

I've added this to our functions.php file but it doesn't seem to work. This should be a global rule with no manual intervention required, right?

@clayton93
Copy link

@972creative I am looking for the same thing. This code won't do that — this code will only apply this to the form with the ID of 1 and the fields with the IDs 1 and 2.

I have been able to very slightly adapt the code to work for all forms by removing the form ID check and checking if the field type is select:

/**
 *  Add a blank option to a Gravity Forms dropdown
 *
 *  @param   object  $form  The Gravity Form
 *  @return  object  $form  The modified Gravity Form
 */
function wp_gravity_forms_add_empty_dropdown_option( $form ) {
    // Loop through the form fields
    foreach( $form['fields'] as &$field ) {

        if( $field->type == 'select' ) {
            // Add blank first element
            $items = array(
                'text' => '',
                'value' => '',
                'isSelected' => true,
                'price' => ''
            );
            // Add to the top of the array
            array_unshift( $field->choices, $items );
        } else {
            continue;
        }
    }
    return $form;
}
add_filter( 'gform_pre_render', 'wp_gravity_forms_add_empty_dropdown_option' );
add_filter( 'gform_pre_submission_filter', 'wp_gravity_forms_add_empty_dropdown_option' );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment