Forked from kjbrum/wp_gravity_forms_add_empty_dropdown_option.php
Created
March 22, 2021 23:23
-
-
Save phillipwilhelm/f23c394c80c0cedb6de63500e214816a to your computer and use it in GitHub Desktop.
Add a blank option to a Gravity Forms select element.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment