I need to send an email from a page when a form is submitted.
I thought I’d use jQuery post but I’m not really sure where to start. Would I use wp_mail()
? And if so how could it be called?
Sorry if that seems vague. Just trying to send an email to the client before a form posts its data to another site;
$('#donationForm').submit(function() {
// send email to client before moving onto worldpay payment form
// send data to worldpay....
this.submit();
});
Basically, you can use JavaScript to post to a WordPress function, then the WordPress function can invoke
wp_mail()
to send the message.A great place to start would be the useful AJAX in Plugins article on the Codex. It walks you through all the steps required to add your JavaScript on the front-end, your PHP on the back-end, and everything you need to do to tie the two together.
First add your email processing function and hook it to
wp_ajax
hooks like this using yourfunctions.php
:Then inside an your theme’s js file create the AJAX call like this:
Now add the nonce to your
footer.php
since it needs to be generated via PHP:And you should be set.