Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Contact Form 7 - add custom function on email send

Just playing around with Wordpress / Contact Form 7.

Is it possible to add custom javascript function on successful email send event?

like image 471
Iladarsda Avatar asked Jul 04 '12 17:07

Iladarsda


People also ask

Can I use submit button in contact form 7?

Submit Button. A submit button is an essential component of a form. As you may know, HTML represents a submit button as an input element with submit type: <input type="submit">. You can use this HTML tag in a contact form of Contact Form 7, but you should use Contact Form 7’s own submit form tag instead.

How to implement custom functionality in contact form 7 plugin for WordPress?

To implement custom functionality in the Contact Form 7 plugin for WordPress. You can use any method from the below methods: This action hook is used to customize form or form value before sending the email using a plugin. In action hooks, we are passing an array that includes the form title and an array of submitted data.

How to call a PHP function after contact form 7 submit?

With wpcf7_before_send_mail action hook, you can call a PHP function after Contact Form 7 submit. I am going to show you how to do that. First, create the custom PHP function that you want to execute after CF7 form submit: The $contact_data variable contains all the form data as an array format.

Is it possible to add custom JavaScript function on successful email send?

Just playing around with Wordpress / Contact Form 7. Is it possible / do you know how to add custom javascript function on successful email send event? simply add you JavaScript function to your page then find the Additional Settings field at the bottom of the contact form management page and use the on_sent_ok JavaScript action hook like so:


2 Answers

Just a quick note that on_sent_ok is deprecated.

Contact form 7 is now using event listeners, like this;

<script type="text/javascript">
  document.addEventListener( 'wpcf7mailsent', function( event ) {
      if ( '11875' == event.detail.contactFormId ) { // if you want to identify the form
       // do something
      }
  }, true );
</script>

Then add this to the wp_footer action.

like this;

add_action( 'wp_footer', 'wp1568dd4_wpcf7_on_sent' );

function wp1568dd4_wpcf7_on_sent() { 
  // the script above
}
like image 148
Chris Pink Avatar answered Nov 10 '22 00:11

Chris Pink


Contact Form 7 emits a number of Javascript events that bubble up to the document object. In version 4.1 they can be found in contact-form-7/includes/js/scripts.js. If you're using jQuery you can access those events like this:

$(document).on('spam.wpcf7', function () {
    console.log('submit.wpcf7 was triggered!');
});

$(document).on('invalid.wpcf7', function () {
    console.log('invalid.wpcf7 was triggered!');
});

$(document).on('mailsent.wpcf7', function () {
    console.log('mailsent.wpcf7 was triggered!');
});


$(document).on('mailfailed.wpcf7', function () {
    console.log('mailfailed.wpcf7 was triggered!');
});
like image 24
likesalmon Avatar answered Nov 09 '22 23:11

likesalmon