繁体   English   中英

提交后下载文件联系表格 7 Wordpress

[英]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,但是“交换”用户的数据:

  • 在您的页面上显示一个 CF7 表单,我在所有案例研究帖子类型中都有相同的表单,我在内容后对其进行了连接
  • 找到一种方法来获取人们想要的 PDF url 供人们下载,至于我所有的案例研究都有不同的 PDF,我只是添加了一个 ACF 字段,仅在 PDF 上过滤,它返回文件 url
  • 基于CF7 Dom 事件,选择您喜欢的操作来进行下载,因为我没有发送任何确认电子邮件,我更喜欢处理 wpcf7submit 事件。 注意 wpcf7submit 事件只有在表单被验证后才会被触发

所以代码看起来像这样:

<?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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM