Javascript 提交后下载文件联系方式7 Wordpress

Javascript 提交后下载文件联系方式7 Wordpress,javascript,wordpress,contact-form-7,Javascript,Wordpress,Contact Form 7,我知道在wordpress中提交联系人表单7构建表单后,如何将表单重定向到另一个页面。例如,我可以将其重定向到pdf文件。(然后在浏览器中打开) 但我想要的是提交后直接下载。没有重定向 我用于将表单重定向到pdf文件的代码是: on_sent_ok: "location = 'url to pdf file here';" 最完美的方式是,表格消失,然后说 在表单位置上显示感谢通知并开始下载PDF文件您可以通过将此.htaccess文件添加到PDF目录中,强制直接下载PDF,而不是在服务器上查

我知道在wordpress中提交联系人表单7构建表单后,如何将表单重定向到另一个页面。例如,我可以将其重定向到pdf文件。(然后在浏览器中打开)

但我想要的是提交后直接下载。没有重定向

我用于将表单重定向到pdf文件的代码是:

on_sent_ok: "location = 'url to pdf file here';"
最完美的方式是,表格消失,然后说
在表单位置上显示感谢通知并开始下载PDF文件

您可以通过将此.htaccess文件添加到PDF目录中,强制直接下载PDF,而不是在服务器上查看

.htaccess-这将强制下载PDF

<FilesMatch "\.(?i:pdf)$">
   ForceType application/octet-stream
   Header set Content-Disposition attachment
</FilesMatch>

由于on_sent_ok已经被弃用,下面是一个可以给你灵感的例子。 我需要在网站所有案例研究内容之后添加下载CTA,但“交换”用户数据用于:

  • 在你的页面上显示一个CF7表单,我在所有案例研究中都有相同的表单,我在内容后面挂了一个
  • 找到一种方法获得想要的PDF url供人们下载,对于我来说,所有案例研究都有不同的PDF,我只是添加了一个ACF字段,仅在PDF上过滤,它返回文件url
  • 基于,选择您希望使加载发生的操作,因为我不发送任何确认电子邮件,我更喜欢处理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 );
} );

发布你的代码,如果你已经有了需要改进的地方,那么它很容易帮助你。我用来在提交到pdf文件后重定向表单的代码是:on_sent_ok:“location='url to pdf file';”
<?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 );
} );