简体   繁体   中英

Contact Form 7 Conditional Redirection Without Sending Mail

I am using Contact Form 7 where I have two fields (amongst others) namely "Service" and "Applicant Pin". What I intend to do is, if some specific Services are chosen along with a matching Applicant Pin, I don't want the form to send email, rather redirect to another page. I have seen a lot of solutions but none work for me. Here is the last code I tried:

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;
                }
    }
    
}

In the developer tools, under the network section, I see a 302 status and fetch / Redirect Type for https://example.com/wp-json/contact-form-7/v1/contact-forms/2852/feedback and the console shows an error

{
    "code": "invalid_json",
    "message": "The response is not a valid JSON response."
}

I am at my wits' end. Can someone please help me?

Without recreating your form, this is what I think would work. The second parameter in the wpcf7_before_send_mail is $abort which is passed by reference. So this stops the execution of the form. If this doesn't work, the other option would be to go the route you were trying, but then use the javascript listener to do a javascript redirect.

I am thinking that it's too late on the flow of things to use the wp_redirect. So the latter may be a better route.

Also... Your comparison function used = which is a setter, and not a comparison.

add_action( 'wpcf7_before_send_mail', 'wpcf7_check_redirection', 10, 3 );
function wpcf7_check_redirection( $contact_form, &$abort, $submission ) {

    if ( 2852 === $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 ) ) {

                $abort = true;
                wp_safe_redirect( 'https://example.com/services/' );
                exit;
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM