How to Store Checkbox Values as Arrays With Post Submissions

Overview

Would you like to store Checkbox values as arrays for your Post Submissions form on a custom meta field? 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.

Red
Green
Blue

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

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

Setup

To implement this, copy and paste the following code snippet to your site.

/**
 * 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 ) {
     
    // Turn checkbox value into an array
    if ( $meta_key === 'test_checkbox' ) {
            $value = explode( "\n", $value );
        }
 
    return $field_value;
}
add_filter( 'wpforms_post_submissions_process_meta', 'wpf_dev_post_submission_process_meta', 10, 5 );

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.

Filter Reference: wpforms_post_submissions_process_meta