WordPress: Simple Back Link Using Referer

   |   By  |  No comments

So, here’s a simple solution to a backlink in WordPress. This makes it easy for someone to return to the previous page, assuming the HTTP_REFERER header is sent by the browser. Thought we’d share this simple solution. We’ve seen

Here’s the function, which you can add to your functions.php file:

/*
* Generate a backlink via HTTP_REFERER (empty if not set by browser).
*/
function get_backlink(){
$ref = wp_get_referer();
$url = get_bloginfo(‘siteurl’);
$urlLen = strlen($url);
$refStr = substr($ref,0,$urlLen);
if(!empty($ref) && strtolower($refStr) == strtolower($url)){
return(‘<a class=”adi_backlink” href=”‘.$ref.'”>< Back</a>’);
}
}

In your template add <?php get_backlink();?> to see the link