繁体   English   中英

联系表格 7 - 重定向到另一个页面

[英]Contact form 7 - Redirect to another page

我的 wordpress 网站上有两种表格。 仅提交一个表单后,我需要将用户重定向到另一个页面。 因此,如果条件为联系表 7 ID,则尝试使用此 javascript。

 document.addEventListener( 'wpcf7mailsent', function( event ) { 
    setTimeout(function(){
        location = 'https://example.com/';
    }, 2500);
}, false );

上面的代码适用于所有页面,因此我以这种方式使用 if 条件对其进行了修改。

document.addEventListener( 'wpcf7mailsent', function( event ) { 
  if('2168' == event.detail.contactFormId){
    setTimeout(function(){
        location = 'https://example.com/';
    }, 2500)  } ;
}, false );

但令人惊讶的是它没有奏效。 有任何想法吗?

上面的代码适用于所有页面,因此我以这种方式使用 if 条件对其进行了修改。

这是低效的,您应该使用以下方法定位显示表单的页面:

add_filter('do_shortcode_tag',  'redirect_form_script', 10,3);
function redirect_form_script($output, $tag, $attrs){
    //check this is your form, assuming form id = 1, replace it with your id.
    if($tag != "contact-form-7" || $attrs['id']!= 2168) return $output;
    $script = '<script>'.PHP_EOL;
    $script .= 'document.addEventListener( "wpcf7mailsent", function( event ){'.PHP_EOL;
    //add your redirect page url.
    $script .= '    location = "http://example.com/submitted/?cf7="'.PHP_EOL;
    $script .= '  }'.PHP_EOL;
    $script .= '</script>'.PHP_EOL;
    return $output.PHP_EOL.$script;
  }

可怜的我。 我使用的是旧版本的联系表 7。插件升级后,它可以工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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