簡體   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