How to Create Custom Plugin in WordPress to Get Referrer URL

Seventy-four percent of online consumers get frustrated when a website promotes content that isn’t applicable to their interests. [1] How can we know customer interests? When new user arrives to website we can get the previous site url that the user visited. Such previously visited page by web visitor is called referrer. If we have browsing history of users from the same or similar websites we can use some patterns for new users.

Since my site is using WordPress platform I decided to build wordpress plugin to be able manipulate content or ads based on web user referrer url. (In the code this term is used as referer) This is my first WordPress plugin and I found creation of it is much easy than I thought.

How to Use Plugin

One of possible use of this wordpress custom plugin (in pseudo code):

if referrer url is google
     show some ad for google visitors 
if referrer url is website ABC
     show some different ad or content
if none of the above
     show some default ad

Below I put the steps that I used to create custom plugin in wordpress. This plugin is just getting user referrer and displaying this information. You would need insert some actions based on referrer url as per example in pseudo code above.

Steps How to Create Custom Plugin in WordPress to Get Referrer URL

1. Go to your web hosting account where wordpress is installed and create folder get_referer_widget under plugins folder. Put blank file init.php in the new created folder. So here how the folder structure will look:
your_blog_name_folder
|—wp-content
|——plugins
|———–get_referer_widget
|—————–init.php

2. In the file init.php we need define name of our plugin, class and register our plugin. The full php source code for file init.php is provided in the end of this post.

3. To get referer we can use wordpress function wp_get_referer:

<?php 
if ( wp_get_referer() )
{
    echo ( wp_get_referer() );
    //  based on referer show some ads or content
 
}
else
{
   echo "default action";
  // do default action as referrer url is not available
}
?>

4. Go to plugins section in wordpress admin and activate newly installed plugin.

get_referer_plugin widget activation
get_referer_plugin widget activation

5. Go to widgets tab and add widget Get Referer Widget for some available area.

Plugin screen view with installed get_referer_widget plugin
Get_referer_widget plugin on the widgets screen

6. Test widget: first time when you access page it will show default option, but then if you click some link on this page you should see the link of this previous page.

7. Now when testing is completed you can put actual actions in it.
8. If you want to stop use this wordpress custom plugin you can deactivate it in wordpress plugin manager.

Here is the source code for init.php

<?php
/*
Plugin Name: Get Referer Widget
Description: This plugin provides a simple widget that shows the referrer url of user
*/
class get_referer_widget extends WP_Widget {
    function Get_Referer_Widget() {
    parent::WP_Widget(false,'Get Referer Widget');
    
    }
    
    function widget($args) {
        
    if ( wp_get_referer() )
    {
        $msg= wp_get_referer() ;
    }
    else
    {
       $msg = "default";
    }

        echo $before_widget;
        echo "<p>$msg</p>";
        echo $after_widget;
       }
    }
    function register_get_referer_widget() {
        register_widget('Get_Referer_Widget');
        
    }
    add_action('widgets_init', 'register_get_referer_widget');
?>

Conclusion
We have seen how to create custom plugin in wordpress to get referrer url. Referring website url can be used to show different ad or content. It can be also used for referral analytics.
References

1. website-personalization-examples-dynamic
2. wp_get_referer
3. How to Create a Custom WordPress Widget
4. WordPress for Dummies. Lisa Sabin-Wilson