How to Store Checkbox Values as an Array

Introduction

Are you interested in storing your checkbox values as an array within the entry and email notifications? Ordinarily, Checkbox fields display selected items individually on separate lines within the entry and notifications.

by default, checkbox selections are stored on a separate line

However, with the approach we’re about to outline, you can ensure that these values are stored as a coherent array. This tutorial will walk you through the necessary steps to achieve this streamlined presentation of checkbox selections. Let’s proceed and optimize your data representation!

Creating the form

To initiate the process, let’s start by crafting a new form and incorporating the essential fields. Ensure that among these fields, at least one Checkbox field is included. This foundation will serve as the basis for implementing the desired functionality. Let’s move forward and set up your form accordingly.

begin by creating your form and adding your fields including at least one checkbox field

If you need help with creating your form, please take a look at this useful guide.

Adding the snippet

Now it’s time to add the snippet that will complete this process for you. If you need assistance with how and where to add snippets to your site, please review this tutorial.

/**
 * Display checkbox values in the email notification only as a one-liner.
 *
 * @link  https://wpforms.com/developers/how-to-store-checkbox-values-as-an-array/
 */
function wpf_inline_checkbox_values( $value, $field, $form_data, $context ) {

	// Check if there are any checkboxes in this submission
	if ( $field['type'] !== 'checkbox' ) {
		return $value;
	}

	// Return these values in a single line separated by a comma
	return str_replace( "\n", ', ', $value );
}
add_filter( 'wpforms_html_field_value', 'wpf_inline_checkbox_values', 10, 4 );

This snippet will take any checkbox value from any WPForms form and display the selections as a single line separated by a comma for the entry as well as the email notification.

Now when a user selects the Checkbox items, it will be stored and emailed in a single line for each checkbox which will be separated by a comma for each selection.

now each checkbox will be stored and emailed in the notification with a single line for each checkbox and their selections separated by a comma. essentially storing the checkbox values as an array

And that’s all you need to store checkbox values as an array! Would you like to also open and close a form based on the time of day? Take a look at our tutorial on How to Schedule a Form Base on the Time of Day.

Filter Reference: wpforms_html_field_value

FAQ

Q: How can I do this for only the email notifications?

A: If you want this functionality for only the email template, first make sure your WPForms Email Settings are set to HTML email. For how to achieve this, please check out this documentation.

Once you’ve confirmed these settings are set to HTML Template, use this snippet to enabled this functionality for only email notifications.

/**
 * Display checkbox values in the email notification only as a one-liner.
 *
 * @link  https://wpforms.com/developers/how-to-store-checkbox-values-as-an-array/
 */
function wpf_inline_checkbox_values( $value, $field, $form_data, $context ) {

	// Check if there are any checkboxes in this submission and that the Email Template is HTML
	if ( $field['type'] !== 'checkbox' || $context !== 'email-html' ) {
		return $value;
	}

	// Return these values in a single line separated by a comma
	return str_replace( "\n", ', ', $value );
}
add_filter( 'wpforms_html_field_value', 'wpf_inline_checkbox_values', 10, 4 );