I know how to redirect the form to another page after submitting a Contact form 7 build form in wordpress. So I can redirect it to a pdf file for example. ( it then opens in the browser)
But what I would like to have is a direct download upon submission. no redirect.
The code I use to redirect the form to the pdf file is:
on_sent_ok: "location = 'url to pdf file here';"
The perfect way it would be that the form will disappear and say a Thank you notice on the place of the form and start the download of the PDF file
You can force a pdf to be downloaded directly instead of being viewed on the server by adding this .htaccess file on the PDF directory.
.htaccess - this will force PDF to be downloaded
<FilesMatch "\.(?i:pdf)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
or you you can try javascript ( untested )
function force_download( file ) {
pdf = window.open(file, '', 'left=100,screenX=100');
pdf.document.execCommand('SaveAs', 'null', 'myfile.pdf');
pdf.close();
}
on_sent_ok: "force_download('pdf_url_here');"
Since the on_sent_ok has been deprecated, here is an example you could use for inspiration. I had the need to add a download CTA after the content of all case studies of a website, but "in exchange" of user's data for:
So the code looks like this:
<?php
// For simplicity, using an anonymous functions
add_action( 'wp_print_footer_scripts', function () {
// Check the wanted singular post type loading
if ( is_admin() || ! is_singular( 'case-study' ) ) {
return;
}
// Check if the ACF PDF field is not empty for use
$pdf_link = get_field( 'pdf' );
if ( empty( $pdf_link ) ) {
return;
}
// Hook on the "wpcf7submit" CF7 Dom event to force the download
printf( "<script>document.addEventListener( 'wpcf7submit', function( event ) { window.open('%s'); }, false );</script>", $pdf_link );
} );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.