Get access to free tutorials, exclusive content and more.

How to Auto-populate Form Fields Using a Query String

You’ve started a brilliant marketing campaign reaching across a variety of platforms: email, Twitter, Facebook, the whole gamut. Everything’s trucking along great. Leads are up, sales are up, bosses are happy. What more is there to ask for?

What if I told you there’s a way to make life even easier? There’s a really quick and simple way to learn which of those segments are turning into leads and sales the least and the most so that you can focus your efforts accordingly.

Using Ninja Forms, there is. With the humble query string variable, you can easily identify exactly where a form submission has come from by auto-populating a form field with data contained in the link that the submitter clicked to bring them to the form.

It’s data that you can get through other channels like Google Analytics, but it’s a world simpler, and the data stays directly associated with the stored form submission. Check it out!

What in the world is a URL query string parameter, anyway?

?key=value

There, that’s completely self-explanatory, right? Alright, maybe not. Let’s break it down.

We all know how a URL works. You click on it or type it into your browser’s address bar, and a request is sent to the server that hosts the website for that page. The host server responds with the content of the webpage and bada bing. Your browser displays the page for you.

A query string is an extra bit of information appended onto the URL that gets passed along with the rest of the URL. It takes the form above, opening with ? to identify as a query string and then a key/value pair that contains the relevant information.

You can link multiple key/value pairs using & if you’re so inclined, like this: ?key=value&key=value

So how is this useful?

You can use a query string to collect information about people who visit your website. Let’s run with the example we opened: you’ve got a marketing campaign in the works that reaches out to potential leads on Facebook, Twitter, and via email. To make the most of your time and energy, you want to be able to track which target audience is the most receptive to your current offers and which is less so.

Tracking social campaigns using a query string

Let’s set you up so that anyone clicking through to your form from Facebook is identified as a Facebook lead when you view the form submission.

First, append the query string ?&source=facebook to the end of the url that you publish on your Facebook/Meta post. Next, open the Ninja Form that the link redirects to and follow the steps below.

  • Add a hidden field to the form (other fields, like single-line text, will work)Add a Hidden field to your query string WordPress form
  • Use the merge tag selector icon to the right within the Default Value field to select the querystring merge tag.Add query string meta to the Default Value field via the merge-tag picker
  • Replace the stock “YOUR_KEY” text with your actual key; in this case, source. The Default Value should now read {querystring:source}

Adding Query string to the Default Value of the Hidden field

That’s literally it. It’s that easy.

Now, when someone clicks through your Facebook link and submits the form, you can view the submitted form in Submissions. You’ll be able to see that hidden field, and its default value will be Facebook: the value associated with the key you put in both the URL query string and the hidden field’s default value!

With this trick, you can easily view traffic patterns and keep a record of them in one common location, conveniently associated with the form the information is relevant to! No more digging through information provided by third-party tools, no more wondering which leads are from which source, and no more struggling to associate specific data with specific submissions. Save yourself time and effort with Ninja Forms!