Search code examples
wordpresshookcontact-form-7

Wordpress Contact form 7 Skip email in before_send hook


I want to hook in to form submission and check if the email address field has some data then skip the email sending process through CF7 as I'm handling that externally.

My code is below

function cornerstonePageFlow ($WPCF7_ContactForm) {
if($WPCF7_ContactForm->id == 10770) {
    $wpcf7      = WPCF7_ContactForm::get_current();
    $submission = WPCF7_Submission::get_instance();
    $email = $submission->get_posted_data('email');
    if(!empty($email)) {
        $submission->skip_mail = true;
        
    }
}
return $contact_form;
}
add_action("wpcf7_before_send_mail", "cornerstonePageFlow", 10, 3);

But this is giving me a fatal error. I cant find anything that explains how to use skip_mail in before send hook. Any help is appreciated Thanks


Solution

  • you can add the filter wpcf7_skip_mail

    if($WPCF7_ContactForm->id == 10770) {
        $wpcf7      = WPCF7_ContactForm::get_current();
        $submission = WPCF7_Submission::get_instance();
        $email = $submission->get_posted_data('email');
        if(!empty($email)) {
            add_filter('wpcf7_skip_mail','__return_true');
            
        }
    }
    

    Alternatively You can use skip_mail: on in the additional settings of the form itself.