### [How to Store Checkbox Values as Arrays With Post Submissions](https://wpforms.com/developers/how-to-store-checkbox-values-as-arrays-with-post-submissions/)

**Published:** August 14, 2020
**Author:** Umair Majeed

**Excerpt:** This tutorial will show you how to store checkbox values from a post submission form as an array.

**Content:**

## Introduction

Would you like to store **Checkbox** values as arrays for your **Post Submissions** form on a custom meta field created from [Advanced Custom Fields](https://www.advancedcustomfields.com/ "Advanced Custom Fields Plugin") plugin? With a small PHP snippet, you can store these values in an array for your custom fields.

By default, when custom fields are mapped in the **Post Submission** addon, WPForms stores the data in the custom field in the same format WPForms stores the data in all forms.

An example being for **Checkbox** form fields (and anything with multiple values), WPForms store the values as a single string, with each value on a new line.

`RedGreenBlue`

However, with a small PHP snippet, you can easily change this to have it stored as an array.

`array( 'Red', 'Green', 'Blue')`

## Creating Your Custom Fields

First, we’re going to create a custom field for our posts using the [Advanced Custom Fields plugin](https://www.advancedcustomfields.com/ "Advanced Custom Fields Plugin").

If you need more information about setting up custom fields, [please see this article](https://wpforms.com/how-to-use-custom-fields-in-user-submitted-posts/ "How to Use Custom Fields in WordPress Guest Posts").

Remember when setting up your ACF fields, it should be like for like. If you create a custom field for a checkbox, make sure the form field is also a checkbox.

![create your custom fields in your post](https://wpforms.com/wp-content/uploads/2020/08/acf-custom-field.jpg)## Creating the Form

Now it’s time to create your post submission form and add a **Checkbox** field to the form.

![add a checkbox field to your form](https://wpforms.com/wp-content/uploads/2020/08/add-checkbox-to-wpforms.jpg)If you need any assistance in setting up a post submission form, [please see this documentation](https://wpforms.com/docs/how-to-install-and-use-the-post-submissions-addon-in-wpforms/).

Once you’ve added the remaining fields to your form, go to the **Settings** tab and click **Post Submissions** to make sure your custom field is mapped correctly.

![make sure to map any and all custom fields on the Post Submissions tab of your form builder](https://wpforms.com/wp-content/uploads/2020/08/wpforms-setup-custom-field-mapping.jpg)## Adding the Snippet to Store Values as Arrays

To store the values as an array, you’ll just need to copy this snippet to your site.

If you need any help adding snippets to your site, please [see this tutorial.](https://wpforms.com/developers/how-to-add-custom-php-or-javascript-for-wpforms/ "How to Add Custom PHP or JavaScript for WPForms")

```

/**
 * Turn checkbox values into an array.
 *
 * @link https://wpforms.com/developers/how-to-store-checkbox-values-as-arrays-with-post-submissions/
 */
  
function wpf_dev_post_submission_process_meta( $field_value, $meta_key, $field_id, $fields, $form_data ) {
 
    // Only run on my form with ID = 443
    if ( absint( $form_data[ 'id' ] ) !== 443 ) {
            return;
    } 
      
    // The $meta_key name is the same name that shows in the Field Name on Advanced Custom Fields
    if ( $meta_key === 'have_visited' ) {
 
            $field_value = explode( "\n", $field_value );
 
        }
  
    return $field_value;
}
add_filter( 'wpforms_post_submissions_process_meta', 'wpf_dev_post_submission_process_meta', 10, 5 );

```

**Note**: It’s important to note here that in the entries screen of WPForms and in the meta field while editing the post, you’ll still see a checkbox. It’s only when you echo out the values that it will show as an array (on a single line) and we’ll show you that in the next step. Also remember to update the form ID from `443` to match your own form ID. If you need help in where to find your form ID, [please check out this helpful documentation](https://wpforms.com/developers/how-to-locate-form-id-and-field-id/).

## Displaying the Values

Now it’s time to display those values on your single post template. WPForms never recommends making live changes to your theme so it’s best to use a child theme or a custom function to echo this out on your template.

For more information on creating a child theme, [have a look at this excellent article from our friends at WPBeginner. ](https://www.wpbeginner.com/wp-themes/how-to-create-a-wordpress-child-theme-video/ "How to Create a WordPress Child Theme")

![display the values as arrays on your single post template](https://wpforms.com/wp-content/uploads/2020/08/wpforms-display-values.jpg)```

        I've already visited these states in the last 5 years:  

```

For the purpose of this tutorial, we’ve created our child theme and on **single.php** we added this line of code to first make sure values are there and if they are to display them.

When your post is displayed, you’ll see the states are listed in a paragraph format rather than a line-by-line display.

![now your checkbox values will be shown in a single line](https://wpforms.com/wp-content/uploads/2020/08/wpforms-display-custom-field-meta.jpg)And that’s it! You’ve successfully changed how these values are stored in your WordPress database. Would you like to also use images for your **Checkbox** form field labels instead of text? Take a look at our article on [How to Apply Images to Checkbox Labels Using CSS](https://wpforms.com/developers/how-to-apply-images-to-checkbox-labels-using-css/ "How to Apply Images to Checkbox Labels Using CSS").

## Related

Filter Reference: [wpforms\_post\_submissions\_process\_meta](https://wpforms.com/developers/wpforms_post_submissions_process_meta/ "Using the wpforms_post_submissions_process_meta in WPForms")

## FAQ

#### A: Can I use this in other addons as well?

A: Absolutely! For example, if you take the same scenario as mentioned in this tutorial, you could add this to the **User Registration addon** so it’s automatically saved to the user profile page while using ACF to add further information to the WordPress User Profile.

In User Registration addon version 2.8.0 and newer, the filter for storing custom meta values is:

`wpforms_user_registration_process_base_custom_meta_value`

**Note:** In older versions (before User Registration 2.8.0), this filter was named:

`wpforms_user_registration_process_registration_custom_meta_value`

If you’re maintaining a legacy site, you can keep both hooks for backward compatibility.

```

/**
 * Turn checkbox values into an array with the User Registration addon.
 *
 * @link https://wpforms.com/developers/how-to-store-checkbox-values-as-arrays-with-post-submissions/
 */
function wpf_dev_user_submission_process_meta( $field_value, $meta_key = '', $field_id = 0, $field = array(), $form_data = array() ) {

	// The $meta_key name is the same name that shows in the Field Name on Advanced Custom Fields.
	if ( $meta_key === 'have_visited' ) {
		$field_value = explode( "\n", $field_value );
	}

	return $field_value;
}

// User Registration addon 2.8.0+.
add_filter( 'wpforms_user_registration_process_base_custom_meta_value', 'wpf_dev_user_submission_process_meta', 10, 5 );

// Backward compatibility for older versions (before 2.8.0).
add_filter( 'wpforms_user_registration_process_registration_custom_meta_value', 'wpf_dev_user_submission_process_meta', 10, 5 );
```

That’s it! Now your submitted checkbox values will be saved as an array when WPForms maps them to your custom post meta field.

Would you like to learn more about setting up Post Submissions, including mapping Custom Post Meta fields, in our [Post Submissions addon guide](https://wpforms.com/docs/how-to-install-and-use-the-post-submissions-addon-in-wpforms/).[](https://wpforms.com/docs/how-to-install-and-use-the-post-submissions-addon-in-wpforms/?utm_source=chatgpt.com)

**Categories:** Tutorials

**Tags:** PHP

---

