Lets say, I create page1 and page2 in wordpress, in Page1 there is a hyperlink which takes me to page2. How do I ensure that visitor has clicked that hyperlink and have come to page2 and not directly on page2, by typing page2 url in the address bar.

if someone has come directly to page2 I want them to be redirected automatically to page1. In old days I was able to crack this with some javascript and and later also in ASP. If I recollect correctly had used querystring and url redirect.

What is easy way in wordpress? unfortunately in wordpress plugin directory all I am getting is plugins for 404 and 301, Please help me here.

  • Arun, any progress on that question?
    – kaiser
    Sep 12 '13 at 12:36
  • I haven't tried the implementation of answer given. We should close this question as answered.
    – Arun
    Jun 15 '15 at 17:06
if ( ! isset( $_SERVER['HTTP_REFERER'] ) AND 'your_page1' !== $_SERVER['HTTP_REFERER'] )
    exit( wp_redirect( $target, 301 ) );
  • 1
    That's the usual way to do it, but if you're using this a means of security remember the referrer can be faked. You can include some session variable or set a cookie on the referring page only to be read on the target destination in addition..
    – Adam
    Oct 15 '12 at 15:23
  • 1
    Or even more secure pass values from a hidden field on referring page with a nonce?
    – Adam
    Oct 15 '12 at 15:30
  • @userabuser Please add this as answer.
    – kaiser
    Oct 15 '12 at 15:36

As a complimentary addition to Kaiser's answer you can secure things further by using a nonce value. (numbers used once)

Step 1 - create your link with nonce value

Page A (the referring page where your users are coming from)

    $url = "http://example.com/destination-slug/";
    $nonce_url = wp_nonce_url( $url, "secure_this_page_nonce");
    echo "<a href='".$nonce_url."'>Click me!</a>";

This will create a link like,


Step 2 - verify the nonce value passed from link

Page B (where you users are going once they click the above link)

if (!wp_verify_nonce($_GET['_wpnonce'],'secure_this_page_nonce')) {

   //optionally redirect users back to the page you want them to come from
   $previous_page = 'http://example.com/previous-page/';
   wp_redirect( $previous_page, 301)

} else {

  //continue with normal processing of page


This checks for the existence of a nonce via the $_GET parameter _wpnonce and whether the action/name secure_this_page_nonce is set which equates to this portion of the URL as shown in Step 1 ?_wpnonce=8c9xa6e33e.

If the nonce value is not present and fails its conditional check then you can redirect the user back to the previous page.

If it passes the conditional check then the user has originated from clicking our link on Page A, in which case we render the page as normal.

Good luck!

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.