Get and Show Parameter from URL in Contact Form 7

Tutorial Difficulty Level    

There are times when using the popular Contact Form 7 WordPress plugin where you need to pass a parameter from the URL and into a hidden field or display it in the form.

for example, you might want to do something like this:

so that the field is already populated when the form page loads. There is a plugin that does something like this already

Contact Form 7 Get and Show Parameter from URL

but this hasn’t been updated in a while, so is not guaranteed to work. Let’s look at a much better way to accomplish the same thing.

In Contact Form 7, the default value of an input field is usually derived from the form-tag’s value part. It is, however, possible to get the default values from the context within which the form is placed.

To do this, add default:{source} option to the form-tag from which you want to derive the default value. Available data sources are: get (HTTP GET variables), post (HTTP POST variables), and post_meta (custom fields). Logged-in user information is also available.

For example, you have a field with a name “your-name”:

[[text* your-name]]

 

To get the default value from HTTP GET variables, add default:get option to the form-tag:

[[text* your-name default:get]]

 

The field will obtain its default value from the GET variable with the same name (“your-name”). Try this by accessing the URL of the page of the form with an additional query string:

If it works correctly, you should see “John Smith” already in the appropriate field on page load!

Using the same logic, the custom field value is used for the default value of the field if: you have default:post_meta option in the form-tag; and the page containing the form has a custom field named “your-name”.

But what if you have two or more default options in a single form-tag? Let’s consider this form-tag’s case:

[[text* your-name default:get default:post_meta “Your Name”]]

 

This form-tag has two default options and a value “Your Name”. The options are evaluated from the first to the last. In this example, default:get is evaluated first. If the “your-name” GET variable has a value, it will be used for the default value. If that value is empty, default:post_meta will be evaluated next. If both of these options do not have values, “Your Name” will be used.