Advertisement
Guest User

Untitled

a guest
Aug 27th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. <?php
  2. /**
  3. * Preselect dropdown field based on URL parameter.
  4. *
  5. * @param array $field
  6. * @param array $field_atts
  7. * @param array $form_data
  8. * @return array
  9. */
  10. function wpf_preselect_dropdown( $field, $field_atts, $form_data ) {
  11.  
  12. // Only continue of the form and field are the ones we are looking for
  13. if ( '637' != $form_data['id'] || '5' != $field['id'] ) {
  14. return $field;
  15. }
  16.  
  17. // Only continue if a prefered vehicle was provided
  18. if ( empty( $_GET['prefered-vehicle'] ) ) {
  19. return $field;
  20. }
  21.  
  22. // Check to see if the vehicle provided exists in the dropdown, if it does
  23. // then set it to default.
  24. foreach ( $field['choices'] as $key => $choice ) {
  25. if ( $choice['label'] == $_GET['prefered-vehicle'] ) {
  26. $field['choices'][$key]['default'] = '1';
  27. break;
  28. }
  29. }
  30. return $field;
  31. }
  32. add_filter( 'wpforms_select_field_display', 'wpf_preselect_dropdown', 10 , 3 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement