Created
August 26, 2016 19:34
-
-
Save jaredatch/dec301f07ad074a593eff58bb79b96b8 to your computer and use it in GitHub Desktop.
WPForms preselect dropdown field based on URL parameter
This file contains hidden or 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 | |
/** | |
* Preselect dropdown field based on URL parameter. | |
* | |
* @param array $field | |
* @param array $field_atts | |
* @param array $form_data | |
* @return array | |
*/ | |
function wpf_preselect_dropdown( $field, $field_atts, $form_data ) { | |
// Only continue of the form and field are the ones we are looking for | |
if ( '637' != $form_data['id'] || '5' != $field['id'] ) { | |
return $field; | |
} | |
// Only continue if a prefered vehicle was provided | |
if ( empty( $_GET['prefered-vehicle'] ) ) { | |
return $field; | |
} | |
// Check to see if the vehicle provided exists in the dropdown, if it does | |
// then set it to default. | |
foreach ( $field['choices'] as $key => $choice ) { | |
if ( $choice['label'] == $_GET['prefered-vehicle'] ) { | |
$field['choices'][$key]['default'] = '1'; | |
break; | |
} | |
} | |
return $field; | |
} | |
add_filter( 'wpforms_select_field_display', 'wpf_preselect_dropdown', 10 , 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment