欢迎光临
我们一直在努力

WordPress文章更新邮件通知评论者

这个功能使用到wordpress的自定义事件功能,通过wordpress函数 wp_schedule_single_event()来实现,可以有效避免大量收信者导致的线程阻塞。

将下面的代码复制粘贴到主题的functions.php文件中即可。

//WordPress文章更新邮件通知评论者
//https://www.daimadog.com/10379.html
function notify_commenters_on_post_update($post_id) {
    $post = get_post($post_id);
    $comments = get_comments(array(
        'post_id' => $post_id,
        'status' => 'approve',
    ));

    if ($comments) {
        foreach ($comments as $comment) {
            $comment_author_email = $comment->comment_author_email;
            $subject = '文章更新通知:' . $post->post_title;
            $message = '尊敬的评论者,文章《' . $post->post_title . '》已经更新,请查看最新内容。';

            // 延迟发送邮件
            wp_schedule_single_event(time() + 60, 'send_notification_email', array($comment_author_email, $subject, $message));
        }
    }
}

add_action('save_post', 'notify_commenters_on_post_update');

function send_notification_email($to, $subject, $message) {
    wp_mail($to, $subject, $message);
}

上面的代码通过定时任务来实现,事件执行频率请修改60,单位是秒。

赞(0) 打赏
未经允许不得转载:WORDPRESS大侠 » WordPress文章更新邮件通知评论者

评论 抢沙发

评论前必须登录!

 

更好的WordPress主题

支持快讯、专题、百度收录推送、人机验证、多级分类筛选器,适用于垂直站点、科技博客、个人站,扁平化设计、简洁白色、超多功能配置、会员中心、直达链接、文章图片弹窗、自动缩略图等...

联系我们联系我们

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫

微信扫一扫

登录

找回密码

注册