Wordpress,wp_mail发送两次

时间:2018-05-25 18:29:13

标签: php wordpress email

我使用此代码段在用户的帖子被删除时向用户发送电子邮件,但是它可以正常工作,但发送两次相同的邮件,请提供帮助

[[self missionOperator] startMissionWithCompletion:^(NSError * _Nullable error) {
    if (error){
        ShowMessage(@"Start Mission Failed", error.description, @"", nil, @"OK");
    }else
    {
        ShowMessage(@"Mission Started", @"", @"", nil, @"OK");
    }
}];

2 个答案:

答案 0 :(得分:2)

您挂钩到delete_post的功能会根据需要执行多次。

当您删除帖子时,您还会删除所有修订版本,因此如果帖子有修订版本,则该函数将被执行多次,具体取决于它的数量。

为了避免每次WordPress从数据库中删除帖子时都执行您的功能,您可以使用did_action( $hook )

此函数返回挂钩执行的次数。我们可以通过放置if语句来使用它来解决多次执行问题。

function authorNotification($post_id) {

    global $wpdb;

    $post = get_post($post_id);

    $author = get_userdata($post->post_author);

    $message = "Hi ".$author->display_name.", We are sorry to inform you that your article, ".$post->post_title." has been declined. We strongly recommend you to go through the guest posting guidelines before you submit an article again.";

    if (did_action('delete_post') === 1){
        //only send once
        wp_mail($author->user_email, "Declined", $message);
    }
}

答案 1 :(得分:0)

无法真正说出这里出了什么问题。 功能似乎工作正常。

我怀疑你在代码中的某个地方调用了authorNotification()函数两次,也许delete_post会被调用两次?

相关问题