2013-11-01 23 views
1

我有一個簡單的表單。我需要不同的促銷代碼將您重定向到不同的頁面。例如:1234會將您發送到example.com。 2323會將您發送到newwebsite.com。我知道如何使用表單和php來做到這一點,但我需要客戶端能夠輸入代碼並重定向自己。似乎沒有任何插件能夠滿足我的需求。我使用引力形式,但仍然需要客戶端編輯模板文件。這裏是我的代碼:使用表格促銷代碼的PHP開關 - 在Wordpress中

<form action="switch.php" method="post"> 
    <input type="text" name="codes" id="codes" /> 
    <input type="submit" /> 

    <?php $i = $_POST; ?> 
    <?php switch($_POST['codes']) { 
     case "1234": 
      header("Location: http://www.myfoursquare.net/archives"); 
      break; 
     case "2222": 
      header("Location: http://www.myfoursquare.net/archives"); 
      break; 
     case "3333": 
       header("Location: http://www.myfoursquare.net/archives"); 
      break; 
      } 
    ?></form> 

這個按照預期工作。不知道如何使這個WordPress的客戶端友好。 謝謝。

回答

0

您可以添加Meta Box,選擇only shows up when a given Page Template。其中,用戶輸入一系列的一些可重複字段代碼 + 重定向URL

然後,在模板頁面,這將是類似於:

# Gets the array of CODE/URL pairs 
$all_codes = get_post_meta($post->ID, 'all_codes', true); 
# Get the URL correspondent to the posted 'codes' 
$redirect = $all_codes[ $_POST['codes'] ]; 
# Do the redirect 
header("Location: $redirect"); 

$all_codes結構實際上取決於元盒是如何實現的。
This search query有許多Meta Boxes的例子。
this other如何處理可重複的字段。

插件Advanced Custom Fields自動化所有這些,可重複的功能是一個優質的附加組件。 Custom Content Type Manager具有內置的功能。

PS:這是給always validate and sanitize所有的用戶輸入

+0

重要這看起來容易得多,比我試圖只是嘗試(在頁面上一堆不同的自定義字段)。如果我使用自定義內容類型管理器可重複的字段,你能建議這是如何適合的。有兩個單獨的文本框...一個代碼和一個url? –

+0

你真的必須玩它才能做到最好。你有本地安裝測試嗎?但正常情況是:1)添加可重複字段2)配置子字段3)檢查插件文檔如何提取數據 – brasofilo

+0

我編輯了上面的代碼,如果有可能希望連接點。 –