N
N
Ninja Mate2016-02-09 09:23:12
JavaScript
Ninja Mate, 2016-02-09 09:23:12

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;
  }
}

There is such a function in js. Replies 0 from the server.
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);
           }
       );
    },


I took an example to follow here

Help me figure it out.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Khokhlov, 2016-02-09
@victorzadorozhnyy

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 question

Ask a Question

731 491 924 answers to any question