我的 WordPress 网站中有两种表单。我需要在仅提交一个表单后将用户重定向到另一个页面。因此尝试使用此 javascript if 条件与联系表单 7 ID。
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 );
但令人惊讶的是它不起作用。有什么想法吗?
最佳答案
Above code applied for all pages so I modified it with if condition this way.
这效率很低,您应该使用以下方式定位显示表单的页面:
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;
}
关于javascript - 联系表格 7 - 重定向到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60885762/