首先添加电子邮件处理功能并将其挂接到wp_ajax
使用functions.php
:
// if you want only logged in users to access this function use this hook
add_action(\'wp_ajax_mail_before_submit\', \'mycustomtheme_send_mail_before_submit\');
// if you want none logged in users to access this function use this hook
add_action(\'wp_ajax_nopriv_mail_before_submit\', \'mycustomtheme_send_mail_before_submit\');
// if you want both logged in and anonymous users to get the emails, use both hooks above
function mycustomtheme_send_mail_before_submit(){
check_ajax_referer(\'my_email_ajax_nonce\');
if ( isset($_POST[\'action\']) && $_POST[\'action\'] == "mail_before_submit" ){
//send email wp_mail( $to, $subject, $message, $headers, $attachments ); ex:
wp_mail($_POST[\'toemail\'],\'this is the email subject line\',\'email message body\');
echo \'email sent\';
die();
}
echo \'error\';
die();
}
然后在主题的js文件中创建如下AJAX调用:
jQuery(\'#donationForm\').submit(function() {
// send email to client before moving onto worldpay payment form
var data = {
action: \'mail_before_submit\',
toemail: $(\'#myemailfield\').val(), // change this to the email field on your form
_ajax_nonce: $(\'#my_email_ajax_nonce\').data(\'nonce\'),
};
jQuery.post(window.location.origin + "/wp-admin/admin-ajax.php", data, function(response) {
console.log(\'Got this from the server: \' + response);
});
});
现在将nonce添加到
footer.php
由于需要通过PHP生成:
...
<span id="my_email_ajax_nonce" data-nonce="<?php echo wp_create_nonce( \'my_email_ajax_nonce\' ); ?>"></span>
<?php wp_footer(); ?>
...
你应该做好准备。