Last active
June 2, 2023 09:12
-
-
Save akhileshpv/2671d6be15f75cc780617cb4c9efcc8b to your computer and use it in GitHub Desktop.
Wordpress WPForms - Preselect the dropdown based on URL parameter
This file contains 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 | |
/** | |
* WPForms - Preselect dropdown based on URL parameter. | |
* | |
* @param array $field | |
* @param array $form_data | |
* @return array | |
*/ | |
function wpforms_preselect_dropdown( $field, $form_data ) { | |
// Only need to continue if the form and field that we are looking for | |
// Also only need to continue if the URL parameter available in the request | |
if('103' == $form_data['id'] && '5' == $field['id'] && isset($_GET['url_param']) && !empty($_GET['url_param'])){ | |
// Check the parameter value exists in the dropdown, | |
// If it yes then set it to default | |
// Also unset the other options | |
foreach ( $field['choices'] as $key => $choice ) { | |
if ( $choice['label'] == $_GET['venue'] ) { | |
$field['choices'][$key]['default'] = '1'; | |
}else{ | |
unset($field['choices'][$key]['default']); | |
} | |
} | |
} | |
return $field; | |
} | |
// Modify Field before render | |
add_filter( 'wpforms_field_data', 'wpforms_preselect_dropdown', 10 , 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment