我正在使用聯系表 7,其中有兩個欄位(其中包括),即“服務”和“申請者密碼”。我打算做的是,如果選擇了一些特定的服務以及匹配的申請人 Pin 碼,我不希望表單發送電子郵件,而是重定向到另一個頁面。我已經看到了很多解決方案,但沒有一個對我有用。這是我嘗試的最后一個代碼:
add_action( 'wpcf7_before_send_mail', 'wpcf7_check_redirection', 10, 1 );
function wpcf7_check_redirection( $contact_form ) {
//Get the form ID
$form_id = $contact_form->id();
if( $form_id == 2852 ) {
$submission = WPCF7_Submission::get_instance();
$cf7_data = $submission->get_posted_data();
$service = $cf7_data['service'];
$pincode = $cf7_data['applicant_pin'];
$saltLake = array(700064, 700091, 700097, 700098, 700105, 700106, 700059, 700101, 700135, 700102, 700157, 700159);
if ( (($service = "Full Package for Single") || ($service = "Full Package for Couples")) && ( in_array($pincode, $saltLake)) ) {
$contact_form->skip_mail = true;
wp_redirect("https://example.com/services/");
exit;
}
}
}
在開發者工具中,在網路部分下,我看到一個 302 狀態并為https://example.com/wp-json/contact-form-7/v1/contact-forms/2852/feedback獲取/重定向型別和控制臺顯示錯誤
{
"code": "invalid_json",
"message": "The response is not a valid JSON response."
}
我沒辦法。有人可以幫幫我嗎?
uj5u.com熱心網友回復:
在您的版本中,您似乎嘗試使用比較函式=,它是一個 setter(x 將等于值),而不是比較(如果 x 等于值)。
我使用wpcf7_skip_mail過濾器來阻止郵件,但還使用wpcf7mailsentjavascript 事件偵聽器添加了重定向腳本以包含在頁腳中。
add_action( 'wpcf7_before_send_mail', 'wpcf7_check_redirection', 10, 1 );
function wpcf7_check_redirection( $contact_form ) {
if ( 2744 === $contact_form->id() ) {
$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
$cf7_data = $submission->get_posted_data();
$service = $cf7_data['service'];
$pincode = $cf7_data['applicant_pin'];
$saltLake = array( 700064, 700091, 700097, 700098, 700105, 700106, 700059, 700101, 700135, 700102, 700157, 700159 );
if ( in_array( $service, array( 'Full Package for Single', 'Full Package for Couples' ), true ) && in_array( $pincode, $saltLake, true ) ) {
// Proper way to add skip mail.
add_filter( 'wpcf7_skip_mail', '__return_true' );
}
}
}
}
add_action( 'wp_print_footer_scripts', 'dd_add_cf7_redirect' );
function dd_add_cf7_redirect() {
if ( is_page( 123 ) ) { // replace with your page id.
?>
<script type="text/javascript">
document.addEventListener('wpcf7mailsent', function (event) {
// Redirect to your location.
location = 'https://www.example.com/your-redirect-uri/';
}, false);
</script>
<?php }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/364462.html
標籤:WordPress的 重定向 联系表格 7
上一篇:用戶正確登錄后如何重定向用戶?
