How to Customize Contact Form 7 in WordPress with Handmade SVG

How to Customize Contact Form 7 in WordPress with Handmade SVG

Final product image
What You’ll Be Creating

In a previous tutorial you learned how to create a responsive handmade SVG form. Today, we’ll go a step further and make it dynamic. 

  • Forms
    How to Build a Responsive Handmade SVG Form
    George Martsoukos

I’ll explain how to integrate this form with WordPress using Contact Form 7 (CF7), arguably the most popular WordPress form plugin.

What We’ll be Building

Here’s a quick video demo of the WordPress form in action:

Note: This tutorial assumes that you are somewhat familiar with WordPress and Contact Form 7.

Assuming that you’ve installed CF7 to a WordPress project, let’s discuss the steps needed for making the form compatible with this plugin.

1. Include SVG Sprites

To begin with we have to include the SVG sprites in our project. 

In the previous tutorial we used a single <svg> element containing all the various elements we needed as <symbol>s, each with a unique id. Whenever we needed one of the elements in our form we’d do so with a <use> element, referencing the id like so:

Now, within our WordPress theme, we’ll create a new PHP file to hold the SVG sprite markup:

As a first step we have to create a new file

We’ll then incorporate this SVG content just after the opening body tag in our theme’s header.php:

Then include it in the header of our theme

2. Create the Contact Form

The second step is to create the form from within WordPress. 

The name of our handmade SVG form

We’ll use a combination of CF7 shortcodes along with some custom HTML.

Two things are important here:

  • Firstly, if you look at the previous tutorial, all form child elements are wrapped within the .handmade-form and .container wrapper elements. Later, when we call the form, we’ll attach these wrapper classes directly to the form element. 
  • Secondly, we cannot 100% recreate the markup for the custom checkboxes with the available CF7 shortcodes. To overcome this limitation we’ll write some custom JavaScript. Plus, for easier manipulation, the .checkbox-list won’t remain a ul, but we’ll convert it into a div.

Here’s the code that we have to add to the CF7 editor:

3. Add the CSS Form Styles

Moving on, we’ll enqueue a new CSS file in the functions.php file of our project, like this (your destination folder may differ though):

Here we’ll put most of the styles from the static demo with some new additions for the editing messages that come with CF7.

Here are all the required styles:

Note: For this example, I’m using a vanilla theme. Depending on your theme though, some of these styles may be overridden. Given that fact, if the form doesn’t appear as expected in your project, be sure to check your theme styles and make the necessary changes. 

4. Add the JavaScript

Coming up next, we’ll enqueue a new JavaScript file in the functions.php file of our project, like this (your destination folder may differ though):

Then, within it, we’ll write the code needed for the checkbox functionality:

Bonus: Customize the Output Messages

Just for fun, let’s add one of our custom SVGs to the AJAX output messages. 

Add a custom SVG to the output message

To do this we won’t use any CSS; instead we’ll take advantage of Contact Form 7’s AJAX events:

5. Call the Contact Form

Last but not least, we’ll include the generated CF7 shortcode in the desired part of our project:

Notice the classes added via the html_class attribute.

Premium Contact Form Plugins

Free plugins are great when you’re getting started with WordPress, but if you want to go to the next level with your sites and save yourself time while you’re doing it, consider a premium contact form plugin from CodeCanyon.

  • WordPress
    Best WordPress Contact Form Plugins
    Nona Blackman
  • WordPress
    Choose a Form Builder: The 7 Best WordPress Form Builders Compared
    Nona Blackman
  • WordPress Plugins
    Create a Drag-and-Drop Contact Form With the FormCraft 3 WordPress Plugin
    Daniel Strongin

You’ve Customized a Contact Form 7 with SVG!

And we’re done folks! In this tutorial we walked through the process of converting a static SVG form into something dynamic by incorporating it into a popular WordPress form plugin. Hopefully the steps were clear to you and you’ll give it a try. 

Have you ever done any customization for Contact Form 7?

If you have any questions or problems, do let me know in the comments below!

Go to Source
Author: George Martsoukos