So I’m back and decided to post some nifty tips I discover along the way.
I am building a form using Contact Form 7 in my client’s site and after submit a file download should be initiated. So I’m gonna share with you how I was able to resolve this.
First, you have to relate the form with the file download, in my case I only use one form and multiple files. So what I did was to use a dynamic form field using the dynamic text extension – https://wordpress.org/plugins/contact-form-7-dynamic-text-extension/
Added it to my form like this –>
[dynamichidden download_url id:download_url "CF7_DOWNLOADFILE"]
In my functions.php I had to create a shortcode for CF7_DOWNLOADFILE, the function I made determines the file based on the url slug, you can also use a database for this but this switch works for me (for now atleast)
function url2pdf(){ $slug = basename(get_permalink()); switch($slug){ case "this url": $filename = "this.pdf"; break; default: $filename = "pdf-test.pdf"; break; }return $filename; }add_shortcode('CF7_DOWNLOADFILE', 'url2pdf');
Now that I have the file url, I just have to add this to my Additional Settings tab
on_sent_ok: "document.getElementById('formFields').style.display = 'none'; window.open('/folder/'+document.getElementById('download_url').value,'_blank')"
Notice that I also added a .css to hide my form upon submission.
I also wanted my thank you message to have a link to the file, on my Messages tab I added this:
Thank you for your submission. Your download will start shortly, if not please <a href="/folder/[download_url]" target="_blank">click here</a>.
And that’s it! I hope someone finds this blog post and find it useful! I also use Contact Form DB to store the form submission and downloads.
Happy coding!