Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement