Skip to content

Instantly share code, notes, and snippets.

@jadealombro
Last active September 27, 2021 09:06
Show Gist options
  • Save jadealombro/de9be76124264edc5bcb5f1a15894c6e to your computer and use it in GitHub Desktop.
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.
<?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