为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

跃然一笑 2022-06-05 15:50:43
我的 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 );但令人惊讶的是它并没有奏效。有任何想法吗?
查看完整描述

2 回答

?
ITMISS

TA贡献1871条经验 获得超8个赞

以上代码适用于所有页面,因此我以这种方式使用 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;

  }


查看完整回答
反对 回复 2022-06-05
?
元芳怎么了

TA贡献1798条经验 获得超7个赞

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



查看完整回答
反对 回复 2022-06-05
  • 2 回答
  • 0 关注
  • 136 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号