在修改插件时,在wordpress中发送带有admin-ajax.php的电子邮件不起作用

时间:2015-11-25 06:52:55

标签: jquery wordpress

我正在尝试使用admin-ajax.php修改wordpress插件以添加电子邮件发送功能,my_plugin.php中的代码如下所示:

add_action( 'wp_ajax_nopriv_send_email', 'send_email' ); 
add_action( 'wp_ajax_send_email', 'send_email' );

function send_email(){
    echo "send email"; die(); //it could not printed
    if ( count($_POST) > 0 ){
         $to = $_REQUEST['to'];
         $from = $_REQUEST['from'];
         $sub = $_REQUEST['subject'];
         $msg = $_REQUEST['msg'];

        $headers = 'From: myname <myemail@wordpress.com>' . "\r\n";
        wp_mail( $to, $sub, $msg, $headers);
        exit;
    }
    exit;

}

和我的form.php

jQuery.ajax({
            type: 'POST',
            url: '".admin_url('admin-ajax.php')."',  
            action:'send_email',
            data: { to:'reciever@gmail.com',
                    from: 'sender@gmail.com',
                    subject: 'test',
                    msg: 'thank you'
                    },
            success: function (res) {
                     alert('The server responded: ' + res);
                }

        });

问题是,当我发布请求时,它无法输入send_email()函数并且不发送任何电子邮件,但它始终响应0并抛出成功消息警告,例如&#39;服务器响应: 0&#39; ;我在这做什么错了?我被困在这里..

1 个答案:

答案 0 :(得分:2)

问题在于你的行动&#39;参数。它应该是&#34;数据&#34;:

的一部分
jQuery.ajax({
        type: 'POST',
        url: '".admin_url('admin-ajax.php')."',  
        data: { 
                action:'send_email',
                to:'reciever@gmail.com',
                from: 'sender@gmail.com',
                subject: 'test',
                msg: 'thank you'
        },
        success: function (res) {
                alert('The server responded: ' + res);
        }

});