[英]Wordpress send email with callback and response using Jquery
Unfortunately I am a beginner in WP code writing and I am trying to implement a custom mail send form in WP whit a response but I have no idea how to do it. 不幸的是,我是WP代码编写的初学者,我试图在WP中实现自定义邮件发送表单,但没有响应,但我不知道该怎么做。
I have the following code in witch "send_carrier_email_callback" should be the e-mail send function: 我在女巫“ send_carrier_email_callback”中有以下代码应该是电子邮件发送功能:
add_action('wp_ajax_send_carrier_email', 'send_carrier_email_callback');
add_action('wp_ajax_nopriv_send_carrier_email', 'send_carrier_email_callback');
function send_carrier_email_callback(){
global $wpdb;
var_dump($_POST);
add_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );
$to = $_POST['email'];
$subject = $_POST['carriertittle']." állásjelentkezés";
$body = $_POST['massage'];
wp_mail( $to, $subject, $body );
// Reset content-type to avoid conflicts -- https://core.trac.wordpress.org/ticket/23578
remove_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );
function wpdocs_set_html_mail_content_type() {
return 'text/html';
}
}
function get_carier_ation_callback() {
global $wpdb;
$post_id = $_POST['career'];
$meta = get_post_meta($post_id, "slide_options");
$meta_values =$meta[0];
$args = array (
'p' => $post_id,
'post_type' => array( 'enetix_career' ),
);
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post(); ?>
<script>
jQuery(document).ready(function(){
jQuery('#applybutton').click(function(){
jQuery('#applyform').toggle("slow");
});
jQuery('#carrieremailsend').click(function(){
var career_id = jQuery('#carrierid').val();
var name = jQuery('#name').val();
var email = jQuery('#email').val();
var massage = jQuery('#massage').val();
var data = {
action: 'send_carrier_email',
career: career_id,
name: name,
email: email,
massage: massage
}
jQuery.post(ajaxurl, data, function(response) {
});
});
});
In the "jQuery.post(ajaxurl, data, function(response)" I should get a response if the send was successful or not. 在“ jQuery.post(ajaxurl,data,function(response)”中,如果发送成功或失败,我应该得到响应。
Anyone could help me? 有人可以帮助我吗?
Thank you 谢谢
You need to do something like this. 您需要做这样的事情。 Hope it works for you: 希望这对你有用:
add_action('wp_ajax_send_carrier_email', 'send_carrier_email_callback');
add_action('wp_ajax_nopriv_send_carrier_email', 'send_carrier_email_callback');
function send_carrier_email_callback(){
global $wpdb;
$send_email = false;
$to = $_POST['email'];
$subject = 'Your subject goes here';
$message = $_POST['message'];
$send_mail = wp_mail( $to, $subject, $message );
echo $send_mail; //returns true or false
}
function get_carier_ation_callback() {
global $wpdb;
$post_id = $_POST['career'];
$meta = get_post_meta($post_id, "slide_options");
$meta_values =$meta[0];
$args = array (
'p' => $post_id,
'post_type' => array( 'enetix_career' ),
);
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post(); ?>
<script>
jQuery(document).ready(function(){
jQuery('#applybutton').click(function(){
jQuery('#applyform').toggle("slow");
});
jQuery('#carrieremailsend').click(function(){
var career_id = jQuery('#carrierid').val();
var name = jQuery('#name').val();
var email = jQuery('#email').val();
var massage = jQuery('#massage').val();
$.ajax({
type: 'post',
url: ajaxurl,
data: {action: 'send_carrier_email_callback', name: name, email: email, message: message},
success: function(response){
console.log(response); //your response goes here
if(response === true){
//color your div or whatever element green
}else{
//color your div red
}
}
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.