How to add a ‘Source URL’ to Gravity Forms Submissions

Gravity Forms is a popular form builder plugin for WordPress that allows you to create various types of forms. If you’re using Gravity Forms and want to track the source URL with every form submission, you can achieve this by adding a custom code snippet to the functions.php file of your WordPress theme. This article will guide you through the process of populating a hidden field with the source URL data in Gravity Forms.

Create a hidden field in your Gravity Form

To start off we’ll need to add the new field where our data will go. Do this by adding a standard field anywhere in your form.

Then go to Advanced and mark the field as “hidden”. This will ensure users cannot see your form, and the magic of this is all done behind the scenes.

Next you’ll want to set the field to be loaded dynamically with the parameter name ‘SOURCE_URL’.

Add custom functions to your themes functions.php file

Now we need to add a custom function to your WordPress theme. Open the functions.php file in by going to Appearance > Theme Files or by accessing them file editor and add the following code:

// Function to populate the hidden field with the source URL
function populate_source_url($value, $field, $name) {
    if ($name === 'SOURCE_URL' && !empty($_SERVER['HTTP_REFERER'])) {
        $value = esc_url_raw($_SERVER['HTTP_REFERER']);
    }
    return $value;
}
add_filter('gform_field_value', 'populate_source_url', 10, 3);

Now just clear your cache and test out your form! You should see the new data show up in your form entry: