WordPress: check if last visited page was homepage?

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?

Read More

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?

Related posts

2 comments

  1. You can use History object it has back method and you can check if it returns URL of your homepage

    The other option is to use cookies on all your pages

    $.cookie("previousPage", window.location.href, {path:"/"});
    

    and then check on PageA

    if($.cookie("previousPage") == 'yourhomepage') {
       //call animation
    }
    

    notice:
    $.cookie is available if you use jquery cookie plugin which can be found here

    useful links:

  2. The WordPress PHP function get_home_url() gives you the URL of your home page (see documentation). Then in the JavaScript you can use document.referrer to compare with:

    var home_page = '<?php echo get_home_url(); ?>';
    if(home_page == document.referrer) {
      //Animate with jQuery, or whatever it is you want to do.
    }
    

    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.

Comments are closed.