0

I have a form that redirects and opens a "thank you" page on submission, but I would like no one to have access to this page by URL. This page should be accessible from the redirection of the form. The problem is that I tried to do it from .htaccess, but it does not work.

Current URL: mySite.com/thank-you

I would like to mask it as: mySite.com/

I used this code in function.php:

/**
 * Form ---> Thank you page
 *
 */
add_action( 'wp_footer', 'mycustom_wp_footer' );

function mycustom_wp_footer() {
    ?>
    <script type="text/javascript">
        document.addEventListener( 'wpcf7mailsent', function( event ) {
            if ( '6881' == event.detail.contactFormId ) { // Sends sumissions on form idform to the thank you page
                location = '/thank-you/';
            } else { // Sends submissions on all unaccounted for forms to the third thank you page
                // Do nothing
            }
        }, false );
    </script>
    <?php
}

enter image description here

I don't know how to make it possible. Can someone help me please?

Despotars
  • 541
  • 1
  • 8
  • 23
  • You could also check in your Thank You page if a specific parameter from your form is set, if it's not then assume someone tried accessing the page directly and so redirect them to your homepage. – cabrerahector Jan 25 '19 at 13:20
  • I have managed to mount with a script that when sent in the form redirects to a page (Thank you page) but I do not know how to do it so that if you try to enter from another site, it will be re-directed to the main page. Can you help me with this please? – Despotars Jan 25 '19 at 13:25
  • Can you help me with this? – Despotars Jan 28 '19 at 15:19
  • Sure. You'll need to show your code though. See [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) for more details. – cabrerahector Jan 28 '19 at 15:27
  • What code? My code in .htaccess does not working because the server does not read the file, and I do not know what else I can do – Despotars Jan 28 '19 at 15:32
  • To help you we need to see how you actually send people to the *Thank you* page. From what you have said so far, you're doing that via a form. Please share your form's code (and all related/relevant code). – cabrerahector Jan 28 '19 at 15:40
  • Here this is the code that I used to open this page (than you) when you send a contact form – Despotars Jan 29 '19 at 08:34
  • @cabrerahector do you understand my code? Do you have any suggestions for me? – Despotars Jan 29 '19 at 13:21
  • I do. So you're using Contact Form 7 for this. Question: is this contact form on a specific page or is it visible on the entire site? – cabrerahector Jan 29 '19 at 14:12
  • Yes, I'm using contact 7 and this contact form is for contact page only, but I have more contact form, but I want only show than you page for the contact form page (id = 6881) – Despotars Jan 29 '19 at 14:32

1 Answers1

2

One way to prevent direct access to your "Thank You" page is by making sure that the people who get there actually came from your "Contact Us" page.

Try adding this to your theme's functions.php file, read the comments for details:

/**
 * Redirects user to homepage if they try to
 * access our Thank You page directly.
 */
function thank_you_page_redirect() {
    $contact_page_ID = 22; // Change this to your "Contact" page ID
    $thank_you_page_ID = 2; // Change this to your "Thank You" page ID

    if ( is_page($thank_you_page_ID) ) {
        $referer = wp_get_referer();
        $allowed_referer_url = get_permalink( $contact_page_ID );

        // Referer isn't set or it isn't our "Contact" page
        // so let's redirect the visitor to our homepage
        if ( $referer != $allowed_referer_url ) {
            wp_safe_redirect( get_home_url() );
        }
    }
}
add_action( 'template_redirect', 'thank_you_page_redirect' );

Update:

Alternatively, this JavaScript version achieves the same result (which should be more compatible with caching plugins):

function mycustom_wp_head() {
    $home_url = get_home_url();
    $contact_page_ID = 22; // Change this to your "Contact" page ID
    $thank_you_page_ID = 2; // Change this to your "Thank You" page ID

    if ( is_page($thank_you_page_ID) ) :
    ?>
    <script>
        var allowed_referer_url = '<?php echo get_permalink( $contact_page_ID ); ?>';

        // No referer, or referer isn't our Contact page,
        // redirect to homepage
        if ( ! document.referrer || allowed_referer_url != document.referrer ) {
            window.location = '<?php echo $home_url; ?>';
        }
    </script>
    <?php
    endif;
}
add_action( 'wp_head', 'mycustom_wp_head' );
cabrerahector
  • 3,653
  • 4
  • 16
  • 27
  • This is my data: $contact_page_ID=6206; $thank_you_page_ID=7177; but is not working good, because always redirects me to the home screen, I have pasted your code below mine code (add_action( 'wp_footer', 'mycustom_wp_footer' );) How could I ckeck why is not working? – Despotars Jan 31 '19 at 15:16
  • Try this: 1. enable the [error log](https://codex.wordpress.org/Debugging_in_WordPress); 2. add `error_log($referer . " " . $allowed_referer_url);` right before `if ( $referer != $allowed_referer_url ) {`; 3. Visit your contact page and use the contact form to trigger the redirection; 4. Check the contents of your error log. – cabrerahector Jan 31 '19 at 15:50
  • I created the debug.log file in the wp-content folder, I gave it 660 permissions, I configured the wp-config.php file and I activated the debug mode, but it does not write anything to me in the file and I can not see any error reflected in that file – Despotars Jan 31 '19 at 16:36
  • Try setting the permissions to 644 (or 755) and give it another shot. – cabrerahector Jan 31 '19 at 16:37
  • I have put 777 and I have tested several times from private browsing and it does not work – Despotars Jan 31 '19 at 16:39
  • I edit this post and I added the picture with my code in functions.php – Despotars Jan 31 '19 at 16:42
  • That's odd. If the redirection happens it means the code inside the _if_ block is being run and `error_log` should write to your `debug.log` file. Here's a more drastic approach: replace the `error_log(...)` line with `wp_die($referer . " " . $allowed_referer_url);` and try again. If everything goes as expected, you should see a message on screen. – cabrerahector Jan 31 '19 at 16:44
  • ok, when I send the form, the following message appears on the screen: http://myweb.com/contact/ – Despotars Jan 31 '19 at 16:51
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/187671/discussion-between-cabrerahector-and-despotars). – cabrerahector Jan 31 '19 at 16:54