我沿着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;
}
感謝您分享這個 - 我也在尋找類似的事情。但有一個問題:阻止用戶直接進入「最終目的地」網址的是什麼?例如,http://example.com/thankyou_orange/ - 如何禁止並使用戶填寫聯繫表單爲必填項? – 2015-02-06 14:30:45
從CR7 [重定向常見問題](https://contactform7.com/redirecting-to-another-url-after-submissions/),on_sent_ok'將在2017年被刪除,但重定向功能仍應該適用於同一常見問題 – icc97 2017-09-11 08:29:55