Back to Community

How to Set Multiple Default Checkbox Values in Contact Form 7

18 threads Sep 16, 2025 PluginContact form 7

Content

One of the more nuanced challenges when working with Contact Form 7 is pre-selecting multiple checkboxes by default. While setting a single default value is straightforward, users often need to check several options automatically. This guide explains the common problem and provides a clear, working solution.

The Problem: Limited Default Selection

By default, the default:shortcode_attr option in a CF7 checkbox field is designed to handle a single value. When you pass a value like wpcf7_chkbox_products1="Value3" via a shortcode, it will check only that one option. However, many forms require multiple options to be selected by default, such as pre-ticking a user's previously chosen preferences or applying a set of standard options. The native shortcode attribute handling does not support multiple values out of the box.

The Solution: Modify the Shortcode Handler

The most effective solution is to use a custom function in your theme's functions.php file. This function will parse a custom shortcode attribute that contains multiple values, separated by a specific delimiter, and then correctly apply them to the checkbox field.

Here is a step-by-step breakdown:

  1. Define Your Shortcode: When embedding your form, define a custom attribute with multiple values separated by commas.
    [contact-form-7 id="1239" title="My Form" my_checkbox_field="Value1,Value3,Value4"]
  2. Set Up Your Checkbox Field: In your form, use the default:shortcode_attr option.
    [checkbox my-checkbox-field default:shortcode_attr "Value1" "Value2" "Value3" "Value4"]
  3. Add the Custom PHP Code: Place the following code in your theme's functions.php file. This code hooks into the shortcode attributes filter, checks for your custom field, and splits a comma-separated string into an array of values.
    add_filter( 'shortcode_atts_wpcf7', 'custom_multiple_checkbox_defaults', 10, 3 );
    
    function custom_multiple_checkbox_defaults( $out, $pairs, $atts ) {
      // Define the name of your checkbox field
      $my_attr = 'my_checkbox_field';
    
      // Check if our custom attribute exists in the shortcode
      if ( isset( $atts[$my_attr] ) ) {
        // Split the comma-separated string into an array
        $out[$my_attr] = explode( ',', $atts[$my_attr] );
      }
      return $out;
    }

Why This Works

The shortcode_atts_wpcf7 filter allows you to manipulate the attributes passed to the CF7 shortcode. The native behavior expects a single string value. By intercepting this process, we can transform a comma-separated string into an array. Contact Form 7's internal logic for the default:shortcode_attr option is then able to recognize this array and check all values within it against the available checkbox options.

Important Considerations

  • Delimiter: This example uses a comma (,) as a delimiter. Ensure the values in your shortcode do not contain commas within themselves.
  • Field Name: Remember to replace my_checkbox_field in both the shortcode and the PHP function with the actual name of your checkbox field.
  • Testing: Always test this functionality on a staging site before deploying it to a live environment.

This approach provides a robust method for dynamically setting multiple default checkbox selections, enhancing form usability for your site's visitors.

Related Support Threads Support