How to Capture a Marketing Lead Referral Source in Form Submissions with WordPress


In this tutorial, I'll show you how to capture the original referral source of your website visitors and store this information in a hidden field on form submissions using a simple WordPress plugin.

Plugin Overview

The plugin works by:

  • Tracking the referral source when a user visits the site.
  • Storing the referral source in the user's session.
  • Dynamically populating this referral source into hidden form fields on supported form builders like Gravity Forms and WPForms.

Getting Started: Setting Up the Referral Source Tracker Plugin

Step 1: Plugin PHP Code

Here’s the full PHP code for the plugin, which we'll go through step by step:

Step 2: Understanding the PHP Code

1. Tracking the Referral Source:

  • The plugin hooks into the WordPress wp action, which runs after WordPress has loaded. It checks if a referral source is already stored in the user's session. If not, it extracts the HTTP_REFERER to determine where the user came from (Google, Bing, Facebook, etc.).
  • Depending on the source, the referral information is stored in a session variable ($_SESSION['referral_source']).

2. Retrieving the Referral Source:

  • The get_referral_source() function retrieves the referral source from the session. If it's not set, it defaults to "Direct or Other."

3. Populating the Referral Source in Forms:

  • The plugin supports Gravity Forms and WPForms. The referral source is dynamically added as a hidden field using the gform_field_value_referral_source and wpforms_field_value_referral_source filters.

4. Enqueueing JavaScript:

  • A JavaScript file (referral-source.js) is enqueued to pass the referral source to the front-end forms.

Step 3: JavaScript to Handle the Hidden Field

Now let’s write the referral-source.js file that will handle inserting the referral source into a hidden field.

Step 4: Adding the Hidden Field to Your Form

If you're using Gravity Forms or WPForms, you’ll need to add a hidden field to your form where the referral source will be stored. Here’s how:

  • Gravity Forms: Add a hidden field and set its default value to {referral_source}. This will automatically pull in the referral source tracked by the plugin.
  • WPForms: Add a hidden field and use the referral_source as the dynamic population value.

Step 5: Testing the Plugin

  1. Install the plugin by adding the PHP code to a new file in the /wp-content/plugins/ directory of your WordPress installation.
  2. Create a Gravity Form or WPForm with a hidden field for "Referral Source."
  3. Visit your website through different channels (e.g., Google, Facebook) and submit the form.
  4. Check the form entries to ensure that the correct referral source is captured.

With this custom plugin, you can track where your leads are coming from and automatically store that information in your form submissions. This is especially useful for optimizing marketing efforts and understanding which platforms are driving the most traffic to your website.

Feel free to download the plugin code from my GitHub repository and use it in your projects. You can find the code here.

Want to Boost Lead Generation and Enhance Every User Touchpoint?

With the right strategy, you can generate more leads while creating seamless, optimized user experiences at every stage of the journey. Let's work together to drive engagement, boost conversions, and accelerate your business growth.