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.
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.
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.):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.
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 withreadfile
.