简体   繁体   中英

wordpress contact form 7 disable email

I am facing an issue with Contact Form 7 for Wordpress . I want to disable the email notification which i did using

demo_mode: on

At the same time i want to redirect on submit which i used to do using

on_sent_ok: "location = 'http://domain.com/about-us/';" 

Both would work when used individually.But i want to use both at the same time.

I tried doing

    on_sent_ok: "location = 'http://domain.com/about-us/';" 
demo_mode: on

Doesnt seem to work. Kindly advice.

The plugin author has changed as of at least 4.0 the way you should do this again. The skip_mail property is now private :

class WPCF7_Submission {
    private $skip_mail = false;
    ...
}

You should use this filter : wpcf7_skip_mail

For example :

function my_skip_mail($f){
    $submission = WPCF7_Submission::get_instance();
    if(/* YOUR TEST HERE */){
        return true; // DO NOT SEND E-MAIL
    }
}
add_filter('wpcf7_skip_mail','my_skip_mail');

The author of the plugin Contact Form 7 has refactored some of the code for its version 3.9 and since then the callback function for the hook wpcf7_before_send_mail must be written differently.

To prevent Contact Form 7 from sending the email and force it to redirect after the form has been submitted, please have a look at the following piece of code (for version >= 3.9):

add_action( 'wpcf7_before_send_mail', wpcf7_disablEmailAndRedirect );

function wpcf7_disablEmailAndRedirect( $cf7 ) {
    // get the contact form object
    $wpcf7 = WPCF7_ContactForm::get_current();

    // do not send the email
    $wpcf7->skip_mail = true;

    // redirect after the form has been submitted
    $wpcf7->set_properties( array(
        'additional_settings' => "on_sent_ok: \"location.replace('http://example.com//');\"",
    ) );
}

Hook into wpcf7_before_send_mail instead of using the flag .

 add_action("wpcf7_before_send_mail", "wpcf7_disablemail");  

    function wpcf7_disablemail(&$wpcf7_data) {  

        // this is just to show you $wpcf7_data and see all the stored data ..!  
        var_dump($wpcf7_data);  // disable this line

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

    }  

Just an update. The following works in 4.1.1.

 on_sent_ok: "location = 'http://domain.com/about-us/';" 
 demo_mode: on

There might have been a change to contact-form-7, because I wasn't able to access the $skip_mail variable in the WPCF7_Submission object. I looked at the submission.php object in the \\wp-content\\plugins\\contact-form-7\\includes\\submission.php file and found this:

private $skip_mail = false;

Since the variable is private, and there are no getters or setters in the file, you're not going to be able to change it externally. Just change it to this:

public $skip_mail = false;

and then you can change the variable like this in your functions.php file:

add_filter('wpcf7_before_send_mail', 'wpcf7_custom_form_action_url');

function wpcf7_custom_form_action_url( $form)
{
    $submission = WPCF7_Submission::get_instance();

    $submission->skip_mail = true;

}

A reminder, if you update the contact-form-7 plugin, it will probably nullify your change, so keep that in mind.

设置skip_mail: on可以解决问题。

UPDATE WPCF7 ver. 7.5: There is now a filter specifically to handle this.

function my_skip_mail($f){
    $submission = WPCF7_Submission::get_instance();
    $data = $submission->get_posted_data();

    if (/* do your testing here*/){
        return true; // DO NOT SEND E-MAIL
    }
}
add_filter('wpcf7_skip_mail','my_skip_mail');

Simple code

Copy and paste the following code in your activated theme functions.php file.

add_filter('wpcf7_skip_mail','__return_true');

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