Contact Form 7 Code Examples: Master the Plugin

Contact Form 7 is a popular WordPress plugin for creating and managing contact forms on your website. It offers a simple and powerful way to collect information from your visitors through responsive and flexible forms. So, if you're looking to add a contact form to your WordPress site, look no further than Contact Form 7. In this comprehensive guide, we'll walk you through various Contact Form 7 code examples to help you customize your forms and understand the plugin's functionalities better.

WordPress Contact Form Setup: Complete Guide to Creating a Professional Form
WordPress Contact Form Setup: Complete Guide to Creating a Professional Form

Before we dive into the code examples, let's ensure you have the Contact Form 7 plugin installed and activated. Upon activation, you'll find a new 'Contact' option in your WordPress dashboard, where you can create and manage your contact forms.

Website Contact Form
Website Contact Form

Basic Contact Form 7 Code Usage

Initially, you can create a simple contact form using the Contact Form 7 plugin's built-in features. Here's how to create a basic form with the following fields: Name, Email, Subject, Message, and a submission button.

MNyamwezi (@UReagan) on X
MNyamwezi (@UReagan) on X

1. In your WordPress dashboard, go to 'Contact' > 'Add New'.

2. Fill in the form name, and you'll see various fields on the form editor screen.

Html CSS Responsive Form Design With Full Code Free Template
Html CSS Responsive Form Design With Full Code Free Template

Shortcode for Embedding Forms

The shortcode is the gateway to embedding your contact forms into WordPress pages or posts. Here's how to retrieve the shortcode for your newly created form.

3. Find the 'Shortcode' meta box on the right-hand side of your screen.

the contact page for an email marketing and sponsored business opportunity portal, which is now available
the contact page for an email marketing and sponsored business opportunity portal, which is now available

4. Copy the shortcode highlighted in the meta box. It should look something like this: `[contact-form-7 id="xx" title="Contact form 1"]` (replace 'xx' with your actual contact form ID).

5. Paste this shortcode into the content area of your desired page or post, and your contact form should now be visible on your website.

Customizing Form Fields

Customizing Contact Form 7: WordPress Plugin Guide
Customizing Contact Form 7: WordPress Plugin Guide

Contact Form 7 offers numerous customization options for each form field. Let's explore some of them.

6. To customize a field, click on it in the form editor screen. A side panel will appear, allowing you adjust various settings such as field type, size, and validation.

Contact Form Templates | Free Online Forms
Contact Form Templates | Free Online Forms
Klyro - Portfolio & Agency Webflow Template
Klyro - Portfolio & Agency Webflow Template
40 Best Free Inquiry Form Templates 2026 - Colorlib
40 Best Free Inquiry Form Templates 2026 - Colorlib
two screens showing the contact form and email address options for each customer who is on their phone
two screens showing the contact form and email address options for each customer who is on their phone
an image of a computer screen with the contact form on it and a chair next to it
an image of a computer screen with the contact form on it and a chair next to it
Clayo - Contact Website Design Ideas | website template | Design
Clayo - Contact Website Design Ideas | website template | Design
Contact Form Template | FormNX
Contact Form Template | FormNX
20 Beautiful CSS Contact Forms Code Examples - OnAirCode
20 Beautiful CSS Contact Forms Code Examples - OnAirCode
the login page for an email form with red and green buttons on it, which are
the login page for an email form with red and green buttons on it, which are

7. For instance, you can change the default 'Name' field into a 'Text' field by selecting 'Text' in the 'Type' dropdown menu.

Advanced Contact Form 7 Code Examples

Now that we've covered the basics, let's delve into more advanced code examples to further customize your Contact Form 7.

Using Contact Form 7's dynamic population feature, you can pre-fill certain form fields based on the user's activity on your website. Here's an example of how to pre-fill the 'Name' and 'Email' fields based on the user's WP comment form information:

Dynamic Population of Form Fields

Add the following lines of code into your theme's `functions.php` file or a custom plugin:

`
function prefill_contact_form_7( $args, $form ) {
$args['form_tag']['name'] = 'f[your-form-id]';
$args['form_tag']['autocomplete'] = 'on';
$args['form_tag']['class'][] = 'your-custom-class';
}
add_filter( 'wpcf7_form_properties_options', 'prefill_contact_form_7', 10, 2 );'`

Replace `'your-form-id'` with your actual Contact Form 7 ID and `'your-custom-class'` with your desired custom class. This code enables form field pre-filling and adds custom classes for better control over your form's presentation.

Redirecting after Form Submission

By default, Contact Form 7 sends users to a thank you message after a successful form submission. However, you might want to redirect them to another page instead. Here's how to do it:

8. In your form editor screen, find the 'Mail' meta box and click on the 'ਵ' icon next to 'Messages' to expand it.

9. Scroll down to 'On submit' and replace the default thank you message with the following code: `

Thank you for your submission. You will be redirected shortly...

`

10. Add the following line of code below the 'On submit' message ( outside the 'On submit' area): `

[redirect to:"https://www.example.com/redirect-url"]

` (replace `'https://www.example.com/redirect-url'` with the URL you want to redirect the user to).

This code not only displays a thank you message but also redirects the user to the specified URL after form submission.

Contact Form 7 offers a wealth of customization options to ensure your website's contact forms align with your site's design and functionality. By mastering these code examples, you're well on your way to utilizing Contact Form 7 to its fullest potential. Happy coding!