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:

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:

if ( wp_get_referer() )
    echo ( wp_get_referer() );
    //  based on referer show some ads or content
   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

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() ;
       $msg = "default";

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

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.

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

Web API to Save to Pocket App and Instapaper App

As we surf the web we find a lot of information that we might use later. We use different applications (Pocket app, Instapaper, Diigo, Evernote or other apps) to save links or notes what we find.

While many of the above applications have a lot of great features there still a lot of opportunities to automate some processes using web API that many of applications provide now.

This will allow to extend application functionality and eliminate some manual processes.

For Example: You have about 20 links that you want to send to pocket like application.
Another example: When you add link to one application you may be want also save link or note to Pocket app or to Instapaper application.
Or may be you want automatically (through script) extract links from some web sites and save them to your Pocket app.

In today post we will look at few examples that allow you start to do this. We will check how to use Pocket API and Instapaper API with python programming.

API for Pocket App

pocket API Pocket, previously known as Read It Later, is an application and service for managing a reading list of articles from the Internet. It is available on many different devices including web browsers. (Wikipedia)
There is great post[1] that is showing how to set up API for it. This post has detailed screenshots how to get all the needed identification information for successful login.

In summary you need get online consumer key for your api application then obtain token code via script. Then you can access the link that will include token and do authorization of application. After this you can use API for sending links.

Below is the summary python script to send the link to Pocket app including previous steps:

import requests

# can be any for link
redirect_link = ""
# obtain consume key online
#connect to pocket API to get token code
pocket_api ='',
         data = {'consumer_key':consumer_key,

pocket_api.status_code       #if 200, it means all ok.

print (pocket_api.text)

#remove 'code='
token= pocket_api.text[5:]
print (token)
url="" + token + "&redirect_uri=" + redirect_link 

import webbrowser
webbrowser.open_new(url) # opens in default browser
#click on Authorize button in webbrowser

# Once authoration done you can post as below (need uncomment code below)  
pocket_add ='',
       data= {'url': '',
              'access_token': token})
print (pocket_add.status_code)


API for Instapaper

Instapaper is a bookmarking service owned by Pinterest. It allows web content to be saved so it can be “read later” on a different device, such as an e-reader, smartphone, tablet. (Wikipedia)
Below is the code example how to send link to Instapaper. The code example is based on the script that I found on Internet [2]

import urllib, sys

def error(msg):

def main():
api = ''

params = urllib.parse.urlencode({
'username' : "actual_user_name",
'password' : "actual_password",
'url' : "https://www.actual_url",
'title' : "actual_title",
'selection' : "description"


r = urllib.request.urlopen(api, params)

status = r.getcode()

if status == 201:

print('%s saved as %s' % (r.headers['Content-Location'], r.headers['X-Instapaper-Title']))
elif status == 400:
error('Status 400: Bad request or exceeded the rate limit. Probably missing a required parameter, such as url.')
elif status == 403:
error('Status 403: Invalid username or password.')
elif status == 500:
error('Status 500: The service encountered an error. Please try again later')

if __name__ == '__main__':

1. Add Pocket API using Python – Tutorial
2. Instapaper