Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phillipwilhelm/f23c394c80c0cedb6de63500e214816a to your computer and use it in GitHub Desktop.
Save phillipwilhelm/f23c394c80c0cedb6de63500e214816a 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment