prevent/block direct access to a thank you page

If the form is redirecting from one page only, you can easily use wp_get_referer() to check for it and if not, redirect.

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;
} );

Leave a Comment