3

I've figured out how to do something BEFORE sending the mail, but I also need an action to happen AFTER it's been send.

I tried wpcf7_after_send_mail but with no success...

Any help on the matter would be much appreciated.

closed as off-topic by Howdy_McGee Feb 1 '17 at 16:06

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • Can't you use the additional settings in the contact form plugin settings? - contactform7.com/additional-settings – Andy Jul 30 '15 at 13:51
  • I was just reading about that as i'm Googling, but i don't where those settings are. Does this allow to redirect a certain form though? I just need 1 specific form to be redirected, not all forms. – Menno van der Krift Jul 30 '15 at 13:58
  • Maybe you could apply this kind of approach ? – birgire Jul 30 '15 at 14:00
  • Ahh just found it and works like a charm :D My Wordpress is in Dutch, hence why i couldn't find the Additional Fields that easily xD – Menno van der Krift Jul 30 '15 at 14:05
7

For anybody still landing here looking for a way to run some PHP code after the email has been sent, Contact Form 7 has a wpcf7_mail_sent hook for exactly this. Usage looks a little like:

// ...in functions.php
add_action('wpcf7_mail_sent', function ($cf7) {
    // Run code after the email has been sent
});

There is also wpcf7_mail_failed, which lets you hook into when the email fails.

  • Didn't work for me. – Pons Mar 1 '17 at 16:31
  • Which version of CF7 are you using? I have this working using version 4.6.1 with WordPress 4.7.2 – adamtomat Mar 2 '17 at 9:02
  • I think I've found the error in my code: I've wrapped my add_action call with a has_action check. But probably when my code (inside a plugin) is executed, the wpcf7_mail_sent action doesn't exist yet. Remove the has_action check made it work. – Pons Mar 3 '17 at 9:24
  • What is this ($cf7) parameter? do i need this – Kevin.a Mar 15 '17 at 10:48
  • It's an instance of WPCF7_ContactForm, which contains a bunch of information about the form that was submitted. For example, you could get the id of the form with $cf7->id(). You may not need it, in which case you can leave it off until the need arises. Here's a link to the class in GitHub incase you wanted to flick through what it does: github.com/wp-plugins/contact-form-7/blob/master/includes/… – adamtomat Mar 16 '17 at 9:05
5

EDIT:

Please note that as of 2017 'on_sent_ok' is deprecated. This means that your code will stop working at some point in the future (likely by the end of 2017). The recommended solution is using DOM event listeners directly. For example, if you used:

on_sent_ok: "ga( 'send', 'event', 'Contact Form', 'submit' );"

You should replace it with:

document.addEventListener( 'wpcf7mailsent', function( event ) {
    ga( 'send', 'event', 'Contact Form', 'submit' );
}, false );

The JavaScript code can be placed e.g. in the footer of your page.

ORIGINAL ANSWER:

Ok figured it out. In the specific form settings, go to the additional fields tab. Type in the following: on_sent_ok: "location.replace('http://www.website-redirect.com');"

It's working like a charm for me now. Hope this helps other developers in the future as well.

enter image description here

I know the image is in Dutch ... but you can't miss it with the big red lines around the tab name.

  • how we save cookie using Additional Tab? I try this on_submit: "ooid = document.getElementById('customer_orderid').value;document.cookie = 'myorderiiid='+ooid+'; expires=Thu, 18 Dec 2025 12:00:00 UTC; path=/';"; but no cookie saved.. WHY? – Mostafa Apr 13 '17 at 6:29
  • Please note that this was a question from TWO YEARS ago ... – Menno van der Krift Oct 30 '17 at 10:12
  • Sorry, I didn't have enough reputation to add it as a comment at that time, so the only way to include this info was through edit. – Amade Feb 23 '18 at 11:15
2

Using on_sent_ok as explained at contactform7.com/additional-settings can be very useful for adding some simple JavaScript like redirecting to another page.

However it is rather prone to failing due to Javascript conflicts with either your current WordPress theme or one of the other plugins you are using.

on_sent_ok is the absolutely last thing that runs in Contact Form 7, so any interferring Javascript Conflict can stop this completing.

As an alternative, Contact Form 7 has a large number of hooks which are listed at http://hookr.io/plugins/contact-form-7/. These can provide a better targeted, more robust solution.

Not the answer you're looking for? Browse other questions tagged or ask your own question.