Plugin for conditional redirect

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.

Read More

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.

Related posts

Leave a Reply

2 comments

  1. 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)

    <?php
        $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,

    http://example.com/destination-slug/?_wpnonce=8c9xa6e33e
    

    Step 2 – verify the nonce value passed from link

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

    <?php
    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)
       exit;
    
    } 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!