Contact Form 7 provides many features to make your forms as simple or complex as you need. One of these important features is the ability to use Default Values in your form fields.
In this article, we will cover the four main ways that Default Values can be used in Contact Form 7 and how to add them:
- Getting default values from post meta
- Getting default values from HTTP requests
- Getting default values from logged-in user
- Using multiple default value options
By the end of this article, you should be able to utilize these Default Values to improve your Contact Form 7 forms.
Before you begin
To be successful in adding default values, you must first know:
- How to set up a CF7 Skins form
- A basic understanding of HTTP requests
- The list and syntax of of user information stored by WordPress
When to use default values for your forms
It’s useful to implement default values in form fields for a variety of cases:
- Help your user fill out a form faster.
- When your forms target registered users of the site.
- When the form needs to be called with a different default value depending on the referrer of the form.
- When you have a fixed value for a field but still need to display it to the user.
1. Getting default values from post meta
The simplest way of getting default values to your form is getting them from the post meta. There are no conditions to this and you simply set a default value for a form field. To do this, use the default keyword and set the desired default value.
[text* name default:post_meta "Michael Clarke"]
Example
The following form has default values for the Subject and Your Message fields. These default values are defined in post meta.
Form Code
2. Getting default values from HTTP requests
HTTP requests are another method of getting default values for your forms. You can use the value of either the GET request or the POST request to use as the default value in you form.
GET
Use the “get” keyword in the form tagForm Tag Form Tags in Contact Form 7 (CF7) (in the Form Tab) are used to define all CF7 form fields. Each Form Tag is replaced with an HTML element, which represents an input field, when it is displayed in an actual form. to get the default value from HTTP GET variables.
[text* name default:get]
Now, you can access the page containing the form using a similar URL:
http://www.yoursite.com/form-page/?name=Clarke
You will now see the value sent via GET in your form.
Note: The name of the GET variable must be the same as the name of the form tag for this to work.
POST
The use of POST is very similar to that of GET. You have to use the “post” keyword in the form tag to get the default value from HTTP POST variables.
[text* name default:post]
Now, when you access this page via a POST request with the variable “name” sent, you will seet the value of the variable in your form.
Example
The following form will have the default subject value as “Inquiry” when a GET variable with that name is sent. Use the following URL to test this:
https://blog.cf7skins.com/using-default-values-contact-form-7/?your-subject=Inquiry
Form Code
3. Getting default values from logged-in user
If a user is logged in to the site when accessing your form, the details of the user can be used in the form. The user details available are the email address, username, URL, first and last names, nickname and the display name.
Username
Use the user_login keyword to get the current user’s login name as the default value in your form.
[text* name default:user_login]
Email Address
Use the user_email keyword to get the current user’s login email as the default value in your form.
[text* name default:user_email]
URL
Use the user_url keyword to get the current user’s URL as the default value in your form.
[text* name default:user_url]
Name
You can choose any of the names saved by the user as the default values in your form. This may be the first or last name, nickname or the chosen display name.
First Name
[text* name default:user_first_name]
Last Name
[text* name default:user_last_name]
Nickname
[text* name default:user_nickname]
Display Name
[text* name default:user_display_name]
Example
The following form will automatically display the first name, last name, display name and email address of the logged in user, provided a user is logged in. Log in to test this.
Form Code
4. Using multiple default options together
In a single tag, you can combine multiple default options together. To do this, decide which options you want combined and the syntax.
[text* first-name default:get default:user_first_name default:post_meta "Michael"]
The priority order of the default value is first to last. This means that the tag will first look for the variable in the GET variable. If not found, it will then look to use the first name of the logged in user. Only failing that will it revert to the default meta value “Michael”, in this case.
Example
In the following form, there are multiple default values set for the name and subject fields.
The first priority of the name tag is to take the display name of the logged in user. If a user is not logged in, “Anonymous” is set as the name.
For the subject field, the form looks for a subject sent via a GET request. If no variable is sent, the subject is set to “General Inquiry”. You can test this by using the URL:
https://blog.cf7skins.com/using-default-values-contact-form-7/?subject=Purchase
Form Code
Further reading
- Contact Form 7 – Getting Default Values from the Context
- Contact Form 7 – Setting Default Values to the Logged-In User
Now that you know how to use default values in Contact Form 7, you can apply it in your own form.
Tip: Are you a CF7 Skins user yet? Download it for free!
Have questions or need help?
If you are using the free version of CF7 Skins, you can get help via the CF7 Skins community and also the Contact Form 7 Support forum.
If you are using any of our CF7 Skins Add-ons, we provide Premium Email Support to help with your questions and problems.