[英]Download File after submission Contact Form 7 Wordpress
我知道如何在 wordpress 中提交联系表单 7 构建表单后将表单重定向到另一个页面。 例如,我可以将其重定向到 pdf 文件。 (然后在浏览器中打开)
但我想要的是在提交时直接下载。 没有重定向。
我用来将表单重定向到 pdf 文件的代码是:
on_sent_ok: "location = 'url to pdf file here';"
完美的方式是表格将消失并在表格的位置说谢谢通知并开始下载PDF文件
您可以通过在 PDF 目录中添加此 .htaccess 文件来强制直接下载 pdf 而不是在服务器上查看。
.htaccess - 这将强制下载 PDF
<FilesMatch "\.(?i:pdf)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
或者你可以尝试 javascript(未经测试)
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');"
由于 on_sent_ok 已被弃用,这里有一个示例,您可以从中获得灵感。 我需要在网站的所有案例研究的内容之后添加一个下载 CTA,但是“交换”用户的数据:
所以代码看起来像这样:
<?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 );
} );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.