WP 网站有新文章发布时发送邮件通知所有用户

捣鼓了下WordPress发布文章或更新文章会发邮件通知已注册的用户,已经过测试,用户不想给邮箱打扰可以在消息通知中取消掉文章评论该选项

记得把代码放在主题目录下functions.php文件中

//邮件通知
function newPostNotify($post_ID) {
     if( wp_is_post_revision($post_ID) ) return;
     global $wpdb;
     $blogurl   = get_bloginfo('url');    //获取首页链接
     $get_post_info = get_post($post_ID);
     if ( $get_post_info->post_status == 'publish' && $_POST['original_post_status'] != 'publish' ) {
         // 读数据库,获取所有用户的email
         $wp_user_email = $wpdb->get_results("SELECT DISTINCT * FROM $wpdb->users");
         // 依次给每个Email发邮件
        foreach ( $wp_user_email as $email ) {
            $user_id = $email->ID;
            //用户是否接收
            if (!zib_msg_is_allow_receive($user_id, 'posts')) {return;}
            $fsemail = $email->user_email;
             // 邮件标题:博客有新文章
            $subject = 'YOY-博客有新文章';
             // 邮件内容:新文章网址:+ URL
            $message = '您好!'.$email->display_name.'<br>您浏览过的YOY-博客更新了一篇新文章:' . get_permalink($post_ID).'<br><br>如有打扰在<a href="'.$blogurl.'/user" rel="noopener" target="_blank">消息通知</a>中关闭掉文章评论选项即可';
             // 发邮件
            wp_mail($fsemail, $subject, $message);
         }
     }
}
add_action('publish_post', 'newPostNotify');

 

 

温馨提示:本文最后更新于2023-07-29 15:51:06,某些文章具有时效性,若有错误或已失效,请在下方留言
© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容