Skip to content

Instantly share code, notes, and snippets.

@jaredatch
Created August 26, 2016 19:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaredatch/dec301f07ad074a593eff58bb79b96b8 to your computer and use it in GitHub Desktop.
Save jaredatch/dec301f07ad074a593eff58bb79b96b8 to your computer and use it in GitHub Desktop.
WPForms preselect dropdown field based on URL parameter
<?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