I’m fairly new to WordPress, and really need some help on the PHP bit. On my wordpress pages (Let’s call it Page A), I want to call a jQuery animation, but only if the visitor clicked a from my wordpress home page to get to Page A.
So just to be clear, how can I call a jQuery animation on page A given that the previous page was the homepage?
I wanted to use is_home() to test the page the visitor came from. But as far as I understand, is_home() can only be used to test if the current page is the homepage.
Is there a way to get around this?
You can use History object it has
back
method and you can check if it returns URL of your homepageThe other option is to use cookies on all your pages
and then check on PageA
notice:
$.cookie is available if you use jquery cookie plugin which can be found here
useful links:
The WordPress PHP function
get_home_url()
gives you the URL of your home page (see documentation). Then in the JavaScript you can usedocument.referrer
to compare with:You can also get the previous page in PHP using
$_SERVER['HTTP_REFERER']
. Wheter you use JavaScript or PHP the information might not be reliable since it comes from the browser. But if it ain’t important that it’s right every single time it is good enough.Please note that this won’t work if you for some reason have single quotes (
'
) in your homepage URL.