There are certain pages with a certain template in my wordpress site that all of them lead to a single page(S).

I want the content of the single page(S) to change based on where the user has came from.

In other words, I want to be able to detect that from which page the user has come to that single page(S).

Is there an easy way to do this? I'm not an advance user, just creating my first wordpress theme :).


  • Keep in mind that it will be impossible to cache pages if you implement this, as it would show you the content of the first user who visited the page. I would strongly advise that you avoid having the same URL serve different things to different people for anything other than logged in users. You'll also need a default else the site will misbehave when a user is hotlinked – Tom J Nowell Oct 21 '16 at 17:32
  • Can i save the url of the current page when a user clicks on the link to the single page(S) in a global variable? or any other method? – Sadegh Shaikhi Oct 21 '16 at 17:38
  • Where would you save it? HTTP is a stateless protocol, there's no state to save that information in, you could try cookies etc, but they aren't reliable, and if they're turned off they wouldn't work. Short of a JS application you'll need to remove any caching at the server end, meaning the site will be slow and expensive to host. There are far better ways of doing this, and I suspect this is not your actual problem but rather a solution you've devised but don't know how to implement, can you provide some context as to why you want this and what you'd use it for? – Tom J Nowell Oct 24 '16 at 12:01
  • allright, what i'm working on is not a store but there are some products, which if a user want's to buy them has to send a form to the provider. – Sadegh Shaikhi Oct 24 '16 at 14:44
  • a link to the form is available in every product page and i want the user to see the name and the category of the product that they want when they go to the order form. – Sadegh Shaikhi Oct 24 '16 at 14:48

As far I've researched there are two ways in my mind to achieve this and they are-

  1. Database: Save the value of the current page ID in the database for the user and continuously update it. That way you can get the last visited page by a user. You can store multiple page visit stat as serialized data in database. Though it is kinda performance reducer but it's effective. And it saves the data which you can use later for any kinda query.
  2. Session: You can also use session to store the previous page ID and this way you can get the last visited page. At session destruction you can get the data by session_set_save_handler

Hope those above ways are gonna help you.

  • The other way conventional method is to set a cookie or cookies relating to key already-visited pages or page types, etc., then use the data saved in the cookie to alter the display in whatever way upon encountering whatever other conditions. Can't get more specific than that, since would need details about the actual objective - what specifically different users are supposed to experience. – CK MacLeod Oct 21 '16 at 19:04
  • If you're serialising data in the database, use json_encode, but keep in mind you can't query that data as a result. Also sessions are unreliable on a lot of managed WordPress hosts, and sites that are spread across multiple servers. Also bear in mind that a user can open more than 1 tab, as such they can have multiple last pages depending on the context all within the same session – Tom J Nowell Oct 24 '16 at 12:03

You can also query the $_SERVER["HTTP_REFERER"] variable which holds the previous URL

In a theme:

<?php if ("http://example.com/thispage" == $_SERVER["HTTP_REFERER"]) : ?>
<h1>You came from <strong>thispage</strong></h1>
<?php endif; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.