How to hook into Contact Form 7 Before Send

后端 未结 4 540
终归单人心
终归单人心 2020-12-09 18:38

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         


        
4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-09 19:02

    Since WPCF7 5.2 the wpcf7_before_send_mail hook has changed quite a lot. For reference, here is how to work with this hook in 5.2+

    Skip mail

    function my_skip_mail() {
        return true; // true skips sending the email
    }
    add_filter('wpcf7_skip_mail','my_skip_mail');
    

    Or add skip_mail to the Additional Settings tab on your form in the admin area.

    Getting the Form ID or Post ID

    function wpcf7_before_send_mail_function( $contact_form, $abort, $submission ) {
    
        $post_id = $submission->get_meta('container_post_id');
        $form_id = $contact_form->id();
    
        // do something       
    
        return $contact_form;
        
    }
    add_filter( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );
    

    Get user inputted data

    function wpcf7_before_send_mail_function( $contact_form, $abort, $submission ) {
    
        $your_name = $submission->get_posted_data('your-field-name');
    
        // do something       
    
        return $contact_form;
        
    }
    add_filter( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );
    

    Send the email to a dynamic recipient

    function wpcf7_before_send_mail_function( $contact_form, $abort, $submission ) {
    
        $dynamic_email = 'email@email.com'; // get your email address...
    
        $properties = $contact_form->get_properties();
        $properties['mail']['recipient'] = $dynamic_email;
        $contact_form->set_properties($properties);
    
        return $contact_form;
        
    }
    add_filter( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );
    

提交回复
热议问题