1

kind of new to this so not sure how to go about this. The task given to me is to that when a visitor arrives at the site through a link, I need to display to them where they came from.

So if they used Google and clicked on a link in Google to load my site, the site would say something like "Welcome visitor from www.google.com."

First, how to get the information where they came from, I don't really know. I was originally considering using the WP Statistics plugin, but then I was told that the HTTP header would contain this information. I don't really know what the HTTP header is or how it works or if there are exceptions.

What would I use to figure out the referreer, and how can I display that to the user? This site is self-hosted so I have access to all the files via my hosting provider's cPanel.

I am using the "Basic" WordPress theme. Thanks.

1
  • your goal is probably impossible. In any case there is nothing wordpress specific about the question right now. Once you figure out the "how do" of detecting such thing, try to amend your question with the code you have tried yourself, and ask questions about relevant wordpress APIs or concept if you still need help Commented Sep 22, 2017 at 9:56

1 Answer 1

1

WordPress has a function that can extract referrer information (website where user originated from): wp_get_referer().

It will return URL from the originating website. If that is not available it will return FALSE.

This is the simple example PHP function that will display welcome message when called, if the referrer is set:

function wse_280729_show_welcome_message() {
  $ref = wp_get_referer();

  if ($ref) {
    echo "Welcome visitor from '$ref'.";
  }
}

How will you use this is up to you, and depends where you want to display this, and will involve some theme modification and calling this function to show the message (if referrer is set).

3
  • Thanks for that tip. I've been looking into using that function but I think I've arrived at a dead end. Do you know how I could use the value returned from 'wp_get_referer()' and display it to the user? I tried to put it in a shortcode that I could call when the page is loaded but either I'm doing something wrong or it's not going to work.
    – M57
    Commented Sep 21, 2017 at 22:54
  • I have expanded my answer with an example function. It is up to you how to use it, or if you need to adapt it. Commented Sep 22, 2017 at 8:43
  • At his point in time referers are probably blocked from all major sites. In addition this code is awfully insecure Commented Sep 22, 2017 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.