1

我编写了一个自定义 Wordpress 功能,用于在向该页面发布新评论时向用户发送电子邮件。我以为我的代码只有在评论被批准后才会发送电子邮件。但即使评论被 WordPress 标记为垃圾,它似乎也在发送电子邮件。

我的代码:

add_action('comment_post', 'pulse_alert', 11, 2);
function pulse_alert($comment_ID, $approved) {  
    //if the comment is approved
    if ($approved) 
    {
        global $post;
        $username = $post->post_title;      
        $user = get_user_by('login', $username);

        //if the user exists
        if ($user)
        {
            //get pulse config details
            $userid = $user->ID;
            $alerts = get_cimyFieldValue($userid, 'PULSEALERT');
            $emailformat = get_cimyFieldValue($userid, 'PULSEALERTFORMAT');

            if($alerts == 'YES')
            {
                //user details
                $user_info = get_userdata($userid);
                $user_email = $user_info->user_email;

                //for page link
                $email_newpulse_pagelink = $username; 

                //for email title
                $email_newpulse_companyname = get_cimyFieldValue($userid, 'COMPANYNAME');

                //Code for Pulse alert emails
                include_once('email/email_newpulse.php');

                $headers[] = 'From: The PartnerPulse team <hello@partnerpulse.co>';
                $headers[] = 'Bcc: The PartnerPulse team <hello@partnerpulse.co>';



                //Send email
                $mail = wp_mail($user_email, $email_newpulse_subject, $email_newpulse_body, $headers);
            }
        }
    }   
}

似乎 $approved var 不起作用。有任何想法吗?

4

2 回答 2

0

我相信 $approved 对于未批准/批准或“垃圾邮件”将是 0/1

您可以在此页面的大约一半处看到:

http://codex.wordpress.org/Plugin_API/Action_Reference

您的 if 语句正在测试 $approved 以查看它是否为真。如果 $approved 作为“垃圾邮件”出现,它将等同于 true,因为 php 将认为字符串为 true,除非它为空或“0”。

将您的 if 语句更改为 if($approved == 1) 并看看情况如何。

于 2013-05-16T16:19:44.163 回答
0

当我检查源代码时,在数据库中插入评论后立即触发此操作挂钩。所以应该在正确的钩子里。

但实际上,$approvedvar 可以有 3 个值0:1spam.

所以你应该这样尝试:

add_action('comment_post', 'pulse_alert', 11, 2);
function pulse_alert($comment_ID, $approved) {  
    //if the comment is approved
    if ($approved == 1) 
    {

您可以检查功能wp_allow_comment

于 2013-05-16T16:34:33.163 回答