Answer the question
In order to leave comments, you need to log in
How to send email in WP using jQuery?
There was a problem creating a plugin for wp. It is not possible to send an email from the site via ajax
in the ajax-text.php plugin file
$myvars = array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
);
wp_enqueue_script( 'bundle', plugins_url( '/bundle.js', __FILE__ ), array('jquery'), '1.0', true );
wp_localize_script( 'bundle', 'MyAjax', $myvars );
add_action( 'wp_ajax_send_message', 'do_send_message' );
function do_send_message() {
if ( isset($_POST['name']) && isset($_POST['email']) &&isset($_POST['message']) ) {
$name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message'];
$send_to = "[email protected]";
$subject = "Question from " . $name;
$success = wp_mail($send_to,$subject,$message);
if ($succsess) return true;
if (!$succsess) return false;
}
}
handleSubmit(data=this.getFormData()) {
jQuery.post(
MyAjax.ajaxurl,
{
'action': 'send_message',
'name': data.name,
'email': data.mail,
'message': data.text
},
function(response){
alert('The server responded: ' + response);
}
);
},
Answer the question
In order to leave comments, you need to log in
Hook add to wp_ajax_nopriv_{action}
Read more: wp-kama.ru/id_2018/ajax-v-wordpress.html
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question