Last active
September 27, 2021 09:06
-
-
Save jadealombro/de9be76124264edc5bcb5f1a15894c6e to your computer and use it in GitHub Desktop.
This snippet is an example of how to programmatically set a default selected value in a dropdown and multiple choice field in WPForms.
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 | |
add_filter( 'wpforms_field_data', ( $field, $form_data ) { | |
if ( absint( $form_data['id'] ) !== 2067 || absint( $field['id'] ) !== 5 ) { | |
return $field; | |
} | |
$default_field_value = 'Second Choice'; | |
foreach( $field['choices'] as $key => $choice ) { | |
if( $choice['label'] === $default_field_value ) { | |
$field['choices'][$key]['default'] = true; | |
break; | |
} | |
} | |
return $field; | |
}, 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment