我试图使用Ajax通过我创建的Web表单发送电子邮件,但我迷路了。我不知道如何Ajax在Wordpress中的功能。在Wordpress中使用Ajax发送邮件
我首先创建了一个动作
add_action('wp_ajax_siteWideMessage', 'wpse_sendmail');
这是应该检索数据和发送邮件的功能是:
function wpse_sendmail()
{
$for = like_escape($_POST['forwhat']);
$email = like_escape($_POST['email']);
$headers = 'From: '.$email ."\r\n".'Reply-To: '.$email;
$message = like_escape($_POST['message_message']);
$respond = like_escape($_POST['message_email']);
wp_mail("[email protected]", "(OTN) Support: ".$support, $message, $headers);
}
最后,JS是像这样:
$("#contact-send").click(function(){
var forwhat = $("#contact-for").val();
var name = $("#contact-name").val();
var email = $("#contact-email").val();
$.post("<?php echo esc_js(site_url()) ?>", { siteWideMessage:"null", forwhat: forwhat, name: name, email:email }, function(){
console.log("success");
});
});
我不太确定我在这里错过了什么。有人能帮助我理解Wordpress的Ajax过程吗?
UPDATE
我我的代码更新到这个至今:
PHP
add_action('wp_ajax_siteWideMessage', 'wpse_sendmail');
add_action('wp_ajax_nopriv_siteWideMessage', 'wpse_sendmail');
function wpse_sendmail()
{
$for = $_POST['forwhat'];
$email = $_POST['email'];
$headers = 'From: '.$email ."\r\n".'Reply-To: '.$email;
$message = $_POST['message_message'];
$respond = $_POST['message_email'];
/*if(wp_mail("[email protected]", "(OTN) Support: ".$for, $message, $headers))
{
echo "WOOHOO";
}*/
if($for)
{
//Just to see if there is any response.
echo "Whoohoo";
}
die();
}
的js
$("#contact-send").click(function(){
var forwhat = $("#contact-for").val();
var name = $("#contact-name").val();
var email = $("#contact-email").val();
var data = { action:'siteWideMessage', forwhat:forwhat, name:name, email:email };
$.post('<?php echo admin_url("admin-ajax.php"); ?>', data, function(response) {
alert(response);
});
});
Wordpress仍然没有响应我的AJAX命令。我使用了检查元素,并且我看不到任何数据被传递。
它应该是'action:'siteWideMessage''和'url'需要到'/ wp-admin/admin-ajax.php' – Ohgodwhy 2014-10-04 01:57:10
以及'$ support'从哪里来的 – Ghost 2014-10-04 02:00:28
怎么了' like_escape'?这是用来逃避sql – 2014-10-04 02:29:25