How to Customize Contact Form 7 for WordPress: Checkboxes and Radio Buttons

In the past, we’ve discussed different customizations of the WordPress Contact Form 7 (CF7) plugin. Today, we’ll learn how to build custom checkboxes and radio buttons with this plugin.

Sound like a good exercise? Here are two animated images that show how these form elements will look after our custom styling:

The custom checkboxes in action
The custom radio buttons in action

And here is a static HTML demo of what the form components will look like:

This tutorial expects that you are somewhat familiar with WordPress and Contact Form 7.

1. Create the Contact Form

Assuming that we’ve installed (I’m using v5.5.4 at the time of this writing) and activated CF7, the next step is to create the necessary shortcodes from the WordPress dashboard.

First, we’ll add a new group of checkboxes:

The group of checkboxesThe group of checkboxesThe group of checkboxes

This will generate the following shortcode:

Secondly, we’ll add a new group of radio buttons:

The group of radio buttonsThe group of radio buttonsThe group of radio buttons

This will generate the following shortcode:

2. Call the Contact Form

Assuming that we’ve built the form, we’ll incorporate its generated shortcode in the desired part of our project:

Notice the class name that we append to it? We’ll use this to differentiate the forms with custom checkboxes and radio buttons.

Coming up next, let’s use the browser console to examine the markup that Contact Form 7 for WordPress emits for checkboxes and radio buttons:

The generated markup for checkboxesThe generated markup for checkboxesThe generated markup for checkboxes
The generated markup for checkboxes
The generated markup for radio buttonsThe generated markup for radio buttonsThe generated markup for radio buttons
The generated markup for radio buttons

Based on these outputs, we’ll create our own styles. Bear in mind that this markup can change after a CF7 update, thus breaking the associated styles. If this occurs, be sure to check the HTML for changes and modify the styles accordingly.

3. Add the Form CSS Styles

Now that we know the HTML output, let’s add the styles that will make our target form controls look attractive. So, in a CSS file of our project, we’ll place the styles that matter:

Here are the key points to remember:

  • We’ll visually hide the default checkboxes and radio buttons.
  • We’ll define the ::before and ::after pseudo-elements of the .wpcf7-list-item-label elements. Both pseudo-elements will be squares for checkboxes and circles for radio buttons. By default, the ::before pseudo-element will only be visible. The ::after pseudo-element will appear smoothly as soon as its related form control is checked. Plus, it will have the half-width of the ::before pseudo-element and be centered within it. To accomplish things, we’ll take advantage of the adjacent sibling combinator (+).
  • Just for a small accessibility enhancement, we’ll add a red box shadow on the focused inputs.

To better demonstrate things, check out the a CodePen demo at the top of the page. It has all the aforementioned styles and isolated markup from our CF7 form. Again, this markup may differ depending on your CF7’s version and settings. This is extracted with the settings discussed above from a website that uses v5.5.4. 

Conclusion

That’s it, folks! In this short tutorial, we concluded another CF7 customization. Hopefully, you’ve found it beneficial and will give it a try in your future CF7 forms that require custom checkboxes or radio buttons. 

If you want to examine more components built with advanced CSS selectors and the CSS Checkbox Hack Technique, be sure to look at some other tutorials in the library.

CSS Checkbox Hack Tutorials

What other Contact Form 7 for WordPress customizations would you like to see? Leave us a comment on social media. As always, thanks a lot for reading!

Contact Form 7 Tutorials