How to restrict people from visiting Download page except when submitting from Contact Form 7 (WordPress Plugin)?

I am working on a WordPress website. We want to offer a download to all the visitors when they duly fill in the contact form. We want to restrict any other visits to the download link, except when it comes from the contact form.

Right now I am am able to redirect the user to the download page on submit using on_sent_ok: “location = ‘http://example.com/downloads‘;” in the Advanced Options of the Contact Form 7 interface.

Read More

However I can access the same page even when I am not submitting the form. Is there a way to block people from visiting this page, except when they use the form? I want to use the contact form 7 plugin for this purpose, within WordPress Framework.

Related posts

Leave a Reply

2 comments

  1. Definitely possible. I personally hate when people make me share my contact info in order to download something that’s free, so I’d strongly recommend you make that optional.

    BUT, since you really want to do it, I would simply set some sort of ?submit=success suffix to the URL, and if that’s present, show the download link.

    You can add this to your functions.php file to make it work (EDIT: Adjusted so that they shortcode can be reused for other downloads.):

    // Get current page URL and clean it up
    $url_current  = @( $_SERVER["HTTPS"] != 'on' ) ? 'http://'.$_SERVER["SERVER_NAME"] :  'https://'.$_SERVER["SERVER_NAME"];
    $url_current .= ( $_SERVER["SERVER_PORT"] !== 80 ) ? ":".$_SERVER["SERVER_PORT"] : "";
    $url_current .= $_SERVER["REQUEST_URI"];
    $url_clean = array_shift( explode('?', $url_current) );
    $url_success = $url_clean . '?submit=success';
    
    function free_download( $atts ) {
    
        // Get variables
        extract(shortcode_atts(array(  
            'contact_form' => '',
            'download_link' => ''
        ), $atts));
    
        // Modify variables
        $contact_form = do_shortcode('[' . $contact_form . ']');
    
        if ($url_current == $url_success) {
            return $download_link;
        }
        else {
            return $contact_form;
        }
    }
    add_shortcode('free_download', 'free_download');
    

    In your Contact Form 7 settings, follow these instructions to set the appropriate redirect link (same page, but with the ?submit=success suffix): http://wordpress.org/support/topic/plugin-contact-form-7-redirect-to-a-page-after-form-succesful

    In the WP content editor, use the [free_download contact_form='contact-form-7 id="12345" title="whatever"' download_link='<p>Some message here. <a href="http://YOUR-DOWNLOAD-LINK.pdf">Download</a></p>'] shortcode to embed the content.

    Now with this approach, someone who uses the ?submit=success link can still get to the content, so it’s not 100% really protected, but it is obscured so that the average visitor won’t get to it.

  2. The first solution I can think of is to set a cookie within the action for on_sent_ok. Your downloads page would have it’s own template page and it would check if the cookie is set (before get_header or any other output is sent). If the cookie is not set you could display a message indicating downloads are not available without completing the form or do a redirect to the page with the form. If cookie is set, you make the download available. Ideally the download file would be outside the web root and you would send it with readfile.