Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hook into Contact Form 7 Before Send

I have a plugin I am writing that I want to interact with Contact Form 7. In my plugin I added the following action add_action

add_action("wpcf7_before_send_mail", "wpcf7_do_something_else");

function wpcf7_do_something_else(&$wpcf7_data) {

    // Here is the variable where the data are stored!
    var_dump($wpcf7_data);

    // If you want to skip mailing the data, you can do it...
    $wpcf7_data->skip_mail = true;

}

I submitted the contact form but the add_action I had did nothing. I'm unsure how to make my plugin intercept or do something when Contact Form 7 does something. Any, help on how to do this?

like image 236
Naterade Avatar asked Apr 28 '15 17:04

Naterade


People also ask

How do I add a contact number in Contact Form 7?

If you would like to add additional fields, like a phone number or company name, you can do so by utilizing the options across the top. For example, if you are wondering how to add a phone number in Contact Form 7, select the tel option. A window will pop up with several options.


2 Answers

I had to do this to prevent Email from being sent. Hope it helps.

/*
    Prevent the email sending step for specific form
*/
add_action("wpcf7_before_send_mail", "wpcf7_do_something_else");  
function wpcf7_do_something_else($cf7) {
    // get the contact form object
    $wpcf = WPCF7_ContactForm::get_current();

    // if you wanna check the ID of the Form $wpcf->id

    if (/*Perform check here*/) {
        // If you want to skip mailing the data, you can do it...  
        $wpcf->skip_mail = true;    
    }

    return $wpcf;
}

This code assumes that you are running the latest version of CF7 your code above used to work until a couple months ago when they went and did some refactoring of the code. [Apr 28 '15]

like image 140
Musk Avatar answered Oct 03 '22 22:10

Musk


I'd like to add that you could just use the wpcf7_skip_mail filter:

add_filter( 'wpcf7_skip_mail', 'maybe_skip_mail', 10, 2 );

function maybe_skip_mail( $skip_mail, $contact_form ) {

    if( /* your condition */ )
        $skip_mail = true;

    return $skip_mail;

}
like image 29
d79 Avatar answered Oct 03 '22 22:10

d79