Specialist SEO Logo

How to check visitor search queries on enquiries using the gravity forms wordpress plugin

Sometimes it can be really useful to understand what keywords customers search for when making an enquiry so you can understand which keywords work for different types of enquiries, which ones to push and which ones to drop.

In this post we use the gravity forms plugin and makes some changes in the wordpress functions.php file in order to retrieve important search information in the email we recieve when a customer enquires through a web form.

The Gravity Forms plugin

Gravity Forms Contact Form Plugin for WordPress
Gravity forms is a well known and very useful plugin for wordpress that enables you to setup and modify forms quickly and efficently adding the forms section to your wordpress control panel. It comes with a large selection of options including notifications and also enables you to store enquiries in your wordpress install.

See the Gravity Forms website for more information.

Step One: Recording the information in a session

The first thing we need to do is extract the information when they land on a page of a website, we want to do this on the first page they land on and get the refering URL and make sure it only records this for the first page and no other pages. Insert the below at the top of your functions.php wordpress theme file.

<?php session_start();



}; ?>

Step Two: Using a function to extract keywords from the refering URL

We then need a function to understand how the refering URL works if its a search engine, for example Google uses a ‘q=’ to pass on the data in a query, this function simply checks the information from a url and outputs it to the keyword searched for. Include this in your functions.php wordpress theme file.

function search_engine_query_string($url = false) {
if(!$url) {

$url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : false;

if($url == false) {

return '';

$parts = parse_url($url);
parse_str($parts['query'], $query);
$search_engines = array(
'bing' => 'q',
'google' => 'q',
'yahoo' => 'p'

preg_match('/(' . implode('|', array_keys($search_engines)) . ')\./', $parts['host'], $matches);
return isset($matches[1]) && isset($query[$search_engines[$matches[1]]]) ? $query[$search_engines[$matches[1]]] : '';

Step Three: Putting the information into values to pass onto the form

Gravity forms has a simple function you can use to setup values  for forms in your functions.php which is the following

// Example of the Function for Gravity
add_filter('gform_field_value_valuename', 'form_valuename');
function hide_form_valuename($value){
$string= string;
return $string;

The value name is simply the parameter name thats passed onto Gravity forms, We extract the following and put it into a parameter

The Keywords

//gravity keywords
add_filter('gform_field_value_keywords', 'hide_form_keywords');
function hide_form_keywords($value){

$keywords = search_engine_query_string($_SESSION['ref']);
return $keywords;

The Landing Page

//gravity landed page forms
add_filter('gform_field_value_landed', 'hide_form_landed');
function hide_form_landed($value){

if ($_SESSION['landed'] == '/'){$landedpage = 'Homepage';}
else {$landedpage = $_SESSION['landed'];};
return $landedpage;

The Refering Search Engine or Website

// gravity refering url
add_filter('gform_field_value_siteref', 'hide_form_ref');
function hide_form_ref($value){

if(strpos($_SESSION['ref'], "google")) {
$referingurl = 'Google'; }
else if(strpos($_SESSION['ref'], "yahoo")) {
$referingurl = 'Yahoo'; }
else if(strpos($_SESSION['ref'], "bing")) {
$referingurl = 'Bing'; }

else { $referingurl = $_SESSION['ref']; };

return $referingurl;

Step Four: Populating the hidden fields in Gravity forms

The final stage is to configure the gravity forms fields to retrieve the data, we will use hidden fields in order to do this. Firstly go into edit forms and select a hidden field to add to your form

Gravity Forms

In this example I have created 3 hidden fields for Landing Page, Search Keywords and Referer

Gravity Forms Data

Next simply populate the fields in the advanced tabbed section of each field, tick “allow this field to be populated dynamically” and put the value name you selected in the function created in the functions.php file for example on the landing page “gform_field_value_landed” was used and therefore we would use the parameter name “landed” to populate the form. Repeat this with any of the other functions you want to show the enquiry form you receive from clients.

On the notifications section of your form the default is set to show all fields, if you have not set this you will need to add the new hidden fields.

Merge Tags

Try the contact form out, search for a page in Google (you can use the query site:yoursite.com – proceeded by a keyword to do this) You should get something similar to the below on your email.

Gravity Forms Result


Extracting this kind of search data from your enquiries can be a great guideline to which keywords convert to the best kind of enquiries. Although conversions can be tracked with analytics data this method will give you a more in-depth overview to which keywords provide the best return.

If you would like this implemented on your wordpress website by our SEO specialised web designers or would like further information then please don’t hesitate to get in touch!

Leave a Reply

Your email address will not be published. Required fields are marked *