Reputation: 89
I am building a website with WordPress.
I try to get the previous page link with the wp_get_referer
function.
The problem is when the user is refreshing the page the value of this function is empty. How can I solve it?
Upvotes: 1
Views: 2605
Reputation: 2711
After you've gotten the referring value via wp_get_referer()
, you'll need to save it for this user in a cookie.
Add this to your functions.php
:
function getReferer() {
// check for a referer
$referer = wp_get_referer();
// if there is one, save it to a cookie
if (!empty($referer)) {
setcookie("referer_url", $referer, time()+3600);
}
// if no referrer, check for a previously-saved cookie
else {
if (isset($_COOKIE['referer_url'])){
// sweet, get it from the cookie
$referer = $_COOKIE['referer_url'];
}
}
return $referer;
}
Now, wherever you need to try to get the referer somewhere in your template or plugin files:
$referer = getReferer();
Upvotes: 2
Reputation: 125
You can do with $_SERVER['HTTP_REFERER']
link back to previous page
Upvotes: 0