2013-05-09 83 views
1

我建立一個網站,我的一個客戶,他們希望有一個功能到他們的網站像下面的一些事情:如何更改動態聯繫表格7重定向URL - WordPress的

當人們點擊下載鏈接,一個表格將出現(聯繫表格7),並且在訪問者提交他們的詳細信息後,它將被重定向到下載鏈接。

我能夠通過使用以下附加設置聯繫表格7

on_sent_ok: "location = 'http://example.com/';" 

enter image description here

但是重新定向到表單提交後一個新的頁面,他們有10個文件,我需要更改重定向鏈接10次以觸發相應文件的下載。我可以通過使用十個非常髒的聯繫表格來完成。

有沒有什麼辦法可以動態改變重定向URL?

例如,

http://example.com/?id=1 
http://example.com/?id=2 

<?php 

$id = $_GET['id']; 

$url= "http://example.com/id=?". $id; 


?> 

有沒有辦法改變與$網址下面的位置?

on_sent_ok: "location = 'http://example.com/';" 

回答

9

我找到了一種動態更改重定向URL的方法。我按照以下步驟來實現的動態重定向:

  1. 在接觸形式7的另外的設置把下面:

    on_sent_ok:「重定向();」

  2. 我們需要一個隱藏字段來攜帶一段必要的信息。但是,默認情況下聯繫表單7不允許我們創建隱藏字段。開發商SevenSpark已經開發了一個擴展以允許隱藏字段在聯繫表格中。7. http://wordpress.org/extend/plugins/contact-form-7-dynamic-text-extension/ 請下載插件並安裝。您會看到爲聯繫表單7生成了兩個新標籤。這將允許您從$ _GET變量中獲取值。請查看插件頁面上的詳細信息。

    ex。 http://example.com/?foo=「酒吧」

  3. 創建模板頁面或退出頁面模板確定。

  4. 將模板分配到相應的頁面。如果您想使用默認模板,則不需要創建或分配任何模板。

  5. 在編輯器中打開模板文件。

  6. 粘貼以下代碼:

    <script>  
    
        function redirect() { 
    
    
    
         // your hidden field ID 
          var filename = document.getElementById('redFilename').value; 
    
         var url =''; 
    
         //alert(document.getElementById('redFilename').value); 
         if (filename == 'apple') 
         { 
    
    
          url= 'http://example.com/thankyou/'; 
    
    
         } 
         else if (filename == 'orange') 
         { 
         url= 'http://example.com/thankyou_orange/'; 
         }  
    
    
    window.location = url; 
    
         } 
         </script> 
    
  7. 現在瀏覽用GET參數的鏈接。

    ex。http://example.com/?redFilename=「apple」

contact form 7的隱藏字段將捕獲redFilename值。如果表單提交成功,它會重定向到http://example.com/thankyou_orange/頁面

Enjoy !!!!

+0

感謝您分享這個 - 我也在尋找類似的事情。但有一個問題:阻止用戶直接進入「最終目的地」網址的是什麼?例如,http://example.com/thankyou_orange/ - 如何禁止並使用戶填寫聯繫表單爲必填項? – 2015-02-06 14:30:45

+1

從CR7 [重定向常見問題](https://contactform7.com/redirecting-to-another-url-after-submissions/),on_sent_ok'將在2017年被刪除,但重定向功能仍應該適用於同一常見問題 – icc97 2017-09-11 08:29:55

3
add_action('wpcf7_mail_sent', 'ip_wpcf7_mail_sent'); 
function ip_wpcf7_mail_sent($wpcf7) 
{ 
    $on_sent_ok = $wpcf7->additional_setting('ip_on_sent_ok', false); 

    if (is_array($on_sent_ok) && count($on_sent_ok) > 0) 
    { 
     wp_redirect(trim($on_sent_ok[0])); 
     exit; 
    } 
} 
+0

中提到的新的'wpcf7mailsent'事件'on_sent_ok' [[將在2017年被刪除](https://contactform7.com/redirecting-to-another-url-after-submissions/) – icc97 2017-09-11 08:26:11

+0

From我可以從我的嘗試中知道,這不起作用,因爲CF7表單通過AJAX提交,所以這只是重定向該AJAX請求。這意味着AJAX永遠不會返回響應,並且CF7表單更新微調永遠不會停止。 – icc97 2017-09-13 13:53:59

+0

如果您[關閉CF7 JavaScript](https://contactform7.com/loading-javascript-and-stylesheet-only-when - 這是必要/) – icc97 2017-09-13 19:40:37

0

on_sent_ok:從來沒有爲我工作。

我試過類似的東西來跟蹤對話。

var sent = $('#wpcf7-f3224-o1').find('.wpcf7-mail-sent-ok'); 

    if (sent.length) { 
     <?php 
      $page_name = get_the_title(); 
      $the_name = str_replace(' ', '', $page_name); 
     ?> 
     self.location="/merci/?page=<?php echo $the_name ?>"; 
    }; 
0

聯繫表格7重定向到另一個網址後提交2017年更新

首先,你需要更新新版本的聯繫表格7我試試v7.4.9,然後將一個聯繫表短代碼在任何頁面,然後將這個JS腳本頁和變更網址在哪裏需要提交

<script> 
document.addEventListener('wpcf7mailsent', function(event) { 
location = 'http://example.com/'; 
}, false); 
</script> 

後重定向你的頁面欲瞭解更多信息請點擊聯繫表7的官方網站上任何地方https://contactform7.com/redirecting-to-another-url-after-submissions/

+0

如果這是真的,這並不回答重定向到一個*動態*網址 – icc97 2017-09-11 08:24:45

+0

的概念,您需要更新您的聯繫表單7插件來使用這個腳本,因爲我上面提到我嘗試它在v7.4.9上,然後在任何頁面中放置一個聯繫表單短代碼,並將該JS腳本放在頁面上的任何位置,並在JS中更改url,在提交後需要重定向動態頁面 – 2017-09-12 06:05:18

+0

如果您閱讀問題,已經這樣做了,但他們明確不希望爲每個頁面更改表單。他們想要使用GET請求的一部分,或者使用表單中提交的變量。 – icc97 2017-09-12 07:21:49

0

我沿着Javascript路線,這工作...但你然後沒有訪問提交的變量(至少我不明白你會怎麼做)。在這裏,我將我的Javascript代碼移植到了PHP代碼中。

您可以訪問表單中任何隱藏或顯示的輸入,並使用它們構建URL。

有一個必要的技巧做重定向在PHP而不是Javascript,這是你必須turn off the CF7 Javascript as per the doc。把這個在您的wp-config.php

define('WPCF7_LOAD_JS', false); 

然後,你可以把這個在你的主題functions.php

add_action('wpcf7_mail_sent', 'icc97_so_mail_sent', 10, 3); 

/** 
* Ported Javascript code to redirect the CF7 form 
* 
* Have to do it in PHP because we want access to the POSTed data 
* 
* There is a further complication that the default CF7 submission is AJAX 
* Then our WP redirect doesn't do anything 
* So we have to turn off the CF7 Javascript via wp-config.php 
* 
*/ 
function icc97_so_mail_sent($contact_form) { 
    $submission = WPCF7_Submission::get_instance(); 
    $data = $submission->get_posted_data(); 
    // example data: 
    // {"_wpcf7":"11684","_wpcf7_version":"4.9","_wpcf7_locale":"en_GB","_wpcf7_unit_tag":"wpcf7-f11684-p11681-o1","_wpcf7_container_post":"11681","your-name":"Ian","your-organisation":"Stack Overflow","your-email":"[email protected]","your-agreement":"1"} 

    /** 
    * Get an attribute value from the CF7 form 
    * 
    * @param string $name attribute name 
    * @return string  attribute value 
    */ 
    $attr = function($name) use ($data) { 
     $val = $data[$name]; 
     // Dropdown/select values are arrays could be a one element array 
     return is_array($val) ? $val[0] : $val; 
    }; 

    /** 
    * Create URL for downloads page 
    * 
    * @param string $domain e.g. https://example.com, can be blank for relative 
    * @param array $names attributes 
    * @return string   URL e.g. https://example.com/downloads/x/x/x/?data=xxx 
    */ 
    $buildUrl = function ($domain, $names) use ($attr) { 
     $stub = [$domain, 'downloads']; 
     // we want lower case, attributes 
     $path = array_map('strtolower', array_map($attr, $names)); 

     // create an array of the full URL and then join with '/' 
     return join('/', array_merge($stub, $path)); 
    }; 

    $domain = ''; 
    // this requires AJAX to be turned off - see function doc block 
    \wp_redirect($buildUrl($domain, ['your-name', 'your-organisation'])); 
    // exit otherwise CF7 forces it's own redirect back to the original page 
    exit; 
}