Disable direct access

Disable direct access to Thank You pages and redirect

Quite often we may find ourselves in a position where we need to redirect users to a Thank you page after they fill up some form on our website. We may do this for various reasons like:

  1. For analytics purpose to track the conversion rate in case of campaigns (forms on landing pages)
  2. To display some hidden content like download button/link only when the user has filled up the form (ex: display download link to your ebook only after user fills up the subscription form)
  3. To lead the user to other content on your website instead of making the user to stay back on the same page after they fill up the form (ex: redirect the user to a Thank You page with a thank you message followed by more content that the user might be interested in rather than just showing a thank you message below the form or as a pop-up)
  4. To perform some specific actions based on the captured inputs from the form on the landing page (ex: show a special birthday greetings or anniversary greetings based on the captured inputs on the thank you page)
  5. or, you just want to redirect the user to a Thank You page because you feel that’s the best way to notify the user once they fill up a form on your website

For whatever reason you have decided to redirect the user to a Thank You page once they fill up a form on your website and you were successful in doing so either by custom coding or by using form builder plugins like Contact Form 7, WPForms, Ninja forms or by using any of the page builder plugins like Elementor Pro.

After building a thank you page and enabling the redirection, you now need to disable direct access to those thank you pages or in other words show the Thank You page only when redirected from a particular page where the form was present which the user had filled up and hit submit and not show if accessed by any other means.

To do this you can add the following code snippet in your child-theme’s functions.php or use a plugin like Code Snippets by Code Snippets Pro.

TIP:

Make all your edits to functions.php only on child theme or, use a plugin that can execute the php code everywhere on your site. Do not touch functions.php or add any code to your theme files until and unless you are a developer or you know what you are doing.

Code snippet to disable direct access to Thank You page and redirect the user to home page if not referred from desired landing page:

<?php
function wpcb_thankspage_rdt(){

	// Replace ENTER_PAGE_ID with the ID of the thank you page 
	// ex: 123
	if (!is_page(ENTER_PAGE_ID)) {
        return;
    }

	// Replace URL_OF_THE_PAGE with full url of the landing page
	// from where the user will be redirected after filling the form
	// ex: https://www.example.com/marketing/landing-page-01
	// Users from this landing page will be shown the thank you page
    if (wp_get_referer() === 'URL_OF_THE_PAGE') {
        return;
    }

	// We are now on the thank you page
	// but the user did not come from the above referring page
	// so redirect the user to the home page and exit
    wp_redirect(get_home_url());
    exit;
}

// Add this function to 'template_redirect' hook
// so that the WordPress knows to check the conditions in this function
// before determining which template to load
add_action('template_redirect', 'wpcb_thankspage_rdt');

That was very easy, but wait, I have multiple forms from various landing pages that are redirecting to multiple thank you pages. How to modify the above code to make it work for my condition?

Let’s first understand what the above function is doing.

In the above snippet we are making use of ‘template_redirect‘ which is an action hook which executes just before WordPress determines which template page to load.

if (!is_page(ENTER_PAGE_ID)) {
    return;
}

Here, If the current page is not our thank you page then we are instructing the WordPress to display that page to the user.

if (wp_get_referer() === 'URL_OF_THE_PAGE') {
    return;
}

Here, we are checking to see if the current page was referred by our landing page. If yes then we are instructing the WordPress to display that page. So, if our Thank You page was referred by our landing page then that should match with ‘wp_get_referer()’ and hence WordPress should show the Thank You page.

wp_redirect(get_home_url());
exit;

Let’s assume that we have 100 pages on our WordPress website and our thank you page id is 23 and we have accessed that page directly by typing in the URL in the browser. The first if condition will be false as is_page() will be equal to 23. The second if condition will also be false as we are accessing the page directly. Now the WordPress knows that we are on Thank You page but were not referred by our landing page and hence it will execute wp_redirect(get_home_url()) and redirect the user to the Home Page.

Note:

wp_redirect() should almost always be followed by a call to exit; as it does not exit automatically.

Code snippet to disable direct access to multiple Thank You pages and redirect the user to home page if not referred from desired landing pages:

<?php
function wpcb_multiple_thankspages_rdt(){

	// Replace ENTER_PAGE_ID_N with the IDs of all the thank you pages
    // is_page() supports array, hooray! 
	// ex: (123, 156, 178)
	if (!is_page(array(ENTER_PAGE_ID_1, ENTER_PAGE_ID_2, ENTER_PAGE_ID_3))) {
        return;
    }

	// Replace URL_OF_THE_PAGE_N with full url of all the desired landing pages
    if ((wp_get_referer() === 'URL_OF_THE_PAGE_1') || (wp_get_referer() === 'URL_OF_THE_PAGE_2') || (wp_get_referer() === 'URL_OF_THE_PAGE_3')) {
        return;
    }

	// We are now on the thank you page
	// but the user did not come from the above referring page
	// so redirect the user to the home page and exit
    wp_redirect(get_home_url());
    exit;
}

// Add this function to 'template_redirect' hook
// so that the WordPress knows to check the conditions in this function
// before determining which template to load
add_action('template_redirect', 'wpcb_multiple_thankspages_rdt');

Last Modified:

| Published under:

by


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *