Introduction
Would you like to store field values for things like the Dropdown, Checkboxes, and Multiple Choice fields? In this tutorial, we’ll show you how to use PHP to take the field value instead of the field label and store it inside the entry.
Adding the snippets
For this tutorial, we’re going to actually add the snippets to our site first. For assistance with how and where to add snippets, please check out this tutorial.
/** * Show values in Dropdown, checkboxes, and Multiple Choice. * * @link https://wpforms.com/developers/add-field-values-for-dropdown-checkboxes-and-multiple-choice-fields/ */ add_action( 'wpforms_fields_show_options_setting', '__return_true' ); /** * Save choices 'values' instead of 'labels' for the fields with 'Show values' option enabled. * * @link https://wpforms.com/developers/how-to-store-field-values-in-the-wpforms-entry/ */ function wpf_dev_process_filter_choices_values( $fields, $entry, $form_data ) { if ( ! is_array( $fields ) ) { return $fields; } foreach ( $fields as $field_id => $field ) { if ( isset( $field[ 'type' ] ) && in_array( $field[ 'type' ], [ 'checkbox', 'radio', 'select' ], true ) && ! empty( $form_data[ 'fields' ][ $field_id ][ 'show_values' ] ) ) { $value_raw = ! empty( $field[ 'value_raw' ] ) ? $field[ 'value_raw' ] : ''; $field[ 'value_raw' ] = $field[ 'value' ]; $field[ 'value' ] = $value_raw; $fields[ $field_id ] = $field; } } return $fields; }; add_filter( 'wpforms_process_filter', 'wpf_dev_process_filter_choices_values', 10, 3 );
We’ve added two separate snippets. The first snippet is following this tutorial that will allow us to add field values for the Dropdown, Checkboxes, and Multiple Choice fields.
The second snippet takes the form submission and while processing it will grab the field value instead of the field label to store on the entry.
Creating the form
Now it’s time to create your form and add your fields which will include at least one Dropdown, Checkboxes, or Multiple Choice.
If you need any help in creating forms, please review our detailed documentation.
For the purpose of this documentation, we added a Dropdown field for the contact preference. In order to add the field values, just select the field and click the Advanced tab.
Click the button to toggle on the Show Values and then you click back to the General tab to add your field values.
Once you’ve saved the form, you can now see that the field values are stored inside the form entry rather than the field label.
Would you like to also send the field values through using the Zapier addon? Take a look at our tutorial on How to Send Field Values to Excel Using Zapier.
Related
Filter Reference: wpforms_process_filter