我建议为此使用AJAX。您可以对jQuery部分执行以下操作:
<script type="text/javascript">
jQuery(document).ready(function($) {
// Execute when user clicks on the "Submit" button
$(\'#SUBMIT_BUTTON_ID\').on(\'click\', function(e) {
e.preventDefault();
var email = $(\'input[name=email]\').val();
var firstname = $(\'input[name=firstname]\').val();
var familyname = $(\'input[name=familyname]\').val();
var other_names = $(\'input[name=other_names]\').val();
var dob_day = $(\'input[name=dob_day]\').val();
var dob_month = $(\'input[name=dob_month]\').val();
var dob_year = $(\'input[name=dob_year]\').val();
var city_of_birth = $(\'input[name=city_of_birth]\').val();
var country_of_birth = $(\'input[name=country_of_birth]\').val();
var gender = $(\'input[name=gender]\').val();
var formData = new FormData();
formData.append("action", "submit_contact_form");
formData.append("email", email);
formData.append("firstname", firstname);
formData.append("familyname", familyname);
formData.append("other_names", other_names);
formData.append("dob_day", dob_day);
formData.append("dob_month", dob_month);
formData.append("dob_year", dob_year);
formData.append("city_of_birth", city_of_birth);
formData.append("country_of_birth", country_of_birth);
formData.append("gender", gender);
$.ajax({
url: "/wp-admin/admin-ajax.php",
type: "post",
data: formData,
processData: false,
contentType: false,
cache: false,
success: function(resp) {
console.log(\'Message sent!\');
},
error: function(errorThrown) {
console.log(errorThrown);
}
});
});
});
</script>
然后按原样使用您的功能,将电子邮件发送给收件人。还要注意,输入字段必须包含下面的ID,jQuery才能获得输入。在这里使用jQuery/AJAX进行需求检查也更容易。
希望有帮助:)
编辑:
你不是真的在发邮件(!)您只检查邮件是否已发送,而不是运行函数本身。更改此代码位:
if(wp_mail($email_to,$subject,$form_message,$headers)) {
echo json_encode(array("result"=>"complete"));
} else {
echo json_encode(array("result"=>"mail_error"));
var_dump($GLOBALS[\'phpmailer\']->ErrorInfo);
}
为此:
$send_mail = wp_mail($email_to,$subject,$form_message,$headers);
if($send_mail) {
echo json_encode(array("result"=>"complete"));
} else {
echo json_encode(array("result"=>"mail_error"));
var_dump($GLOBALS[\'phpmailer\']->ErrorInfo);
}