简体   繁体   中英

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?

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 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM