Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Capturing the External URL in a Variable and Placing it in a Form - PHP

I'm adding some custom PHP to the functions.php file in a WordPress theme.

I'm trying to capture the the URL of the visitor when they hit the home page using:

function log() {

    if (is_front_page()) {

        global $refferral_url;
        $refferal_url = $_SERVER['HTTP_REFERER'];

    }

add_action('wp_head', 'log');

My idea is that now that I have a global variable, I can take it and use it another function, namely my custom form field. Which is as follows:

//Add Referal URL to Gravity Forms Submission
add_filter("gform_field_value_refurl", "referral_url");

function referral_url($form) {

    global $refferal_url;
    $refurl = $refferal_url;

    //Return that value to the form
    return esc_url_raw($refurl);

}

This code just doesn't work. If I just do this though:

//Add Referal URL to Gravity Forms Submission
add_filter("gform_field_value_refurl", "referral_url");

function referral_url($form) {


    $refurl = $_SERVER['HTTP_REFERER'];

    //Return that value to the form
    return esc_url_raw($refurl);

}

It works, but the value placed in the form is of the page right before the user reached the form, which makes sense. I need the page right before the home page to be captured, whether that's Facebook, Google or monsvenus.com. Great powers of the internet, where am I going wrong? ​

1 Answer

In your first set of code, it doesn't look like any of those variables are actually defined with any values, whereas the $_SERVER['HTTP_REFERER'] will assign the value with its built in capabilities.

$refferal_url doesn't seem to have anything to define its value in your code unless there's more code that's not posted?

Hey Matthew, Thanks for getting back to me and I'm sorry I've been so slow to respond.

That is the entirety of my code, I have tried a few other things but that hasn't worked.

Am I not setting a value to $refferal_url when I do this:

$refferal_url = $_SERVER['HTTP_REFERER'];

?