Disable direct access to a wordpress page

I want to disable direct access to a certain page..

For example:

Read More

I have a contact form that when it is sent it redirects the customer to a thank you page.

Now I want that thank you page to only be accessed through redirection only and if I put the url of the thank you page on the browser I don’t want it to be accessible.

How can I do this with a wordpress page?

Related posts

Leave a Reply

3 comments

  1. An “incomplete” possibility is to check the $_SERVER['HTTP_REFERER'] variable to see if the value is equal to the URL of the contact page which should be taking you to the thank you page.

    I say it’s incomplet because if you would display a link to the thank you page on the contact page and the user would click that link, the HTTP_REFERER would be the contact page.

    Another possibility would be to generate a unique token which you can add to the thank you page URL (and the current session) and the thank you page could check for the existence and correctness of this token.

  2. I think when you submit the contact form , you should save the form in the database and then generate a success code. Now send this success code to the thank you page, which will then check if the code exists, and if yes, thank you page will be displayed. This way you will be sure of two things

    1) Customer view the thank you page only if the customer details are successfully saved.

    2) Customer view the thank you page only if the customer has come from contact page.

    To implement this, you need to create a separate page template for thank you page.

  3. add_action('template_redirect', function(){
    // ID of the thank you page
    if ( ! is_page(12345)) {
        return;
    }
    
    // coming from the form, so all is fine
    if (wp_get_referer() == 'URL_OF_FORM') {
        return;
    }
    
    
    // we are on thank you page
    // visitor is not coming from form
    // so redirect to home
    wp_redirect( get_home_url() );
    exit; }