Last Updated on
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.
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"]
The following form has default values for the Subject and Your Message fields. These default values are defined in post meta.
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.
Use the “get” keyword in the form tag 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:
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.
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.
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:
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.
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]
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]
Use the user_url keyword to get the current user’s URL as the default value in your form.
[text* name default:user_url]
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.
[text* name default:user_first_name]
[text* name default:user_last_name]
[text* name default:user_nickname]
[text* name default:user_display_name]
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.
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.
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:
- 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.
Not a CF7 Skins user yet? Download it for free!