string(11) "newsflashes" string(8) "document" string(4) "shop" string(6) "circle"

7b2主题实现用户发布或更新文章后,通知粉丝

功能介绍

实现后的效果:

  • 当作者发布新文章时,其粉丝会收到站内通知和邮件通知
  • 当作者更新已发布的文章时,其粉丝同样会收到通知
  • 通知包含文章标题和链接,方便粉丝直接查看
7b2主题实现用户发布或更新文章后,通知粉丝

实现步骤

1. 添加代码位置

将以下代码添加到7b2子主题的functions.php文件中。如果您还没有创建子主题,建议先创建一个子主题再进行修改。

2. 完整代码实现

function notify_followers($new_status, $old_status, $post) {
    if (!is_object($post) || $post->post_type != 'post' || $new_status != 'publish') return;
    if (defined('DOING_AUTOSAVE') || wp_is_post_revision($post->ID)) return;
    
    try {
        $author_id = $post->post_author;
        if(!$author_id) return;
        
        global $wpdb;
        $table_name = $wpdb->prefix . 'b2_msg';
        $is_new = ($old_status != 'publish' && $new_status == 'publish');
        
        $followers = $wpdb->get_results($wpdb->prepare(
            "SELECT u.ID, u.user_email, um.meta_value 
            FROM {$wpdb->users} u 
            INNER JOIN {$wpdb->usermeta} um ON u.ID = um.user_id 
            WHERE um.meta_key = %s",
            'zrz_follow'
        ));

        if(empty($followers)) return;

        foreach($followers as $follower) {
            $following = maybe_unserialize($follower->meta_value);
            if(!is_array($following) || !in_array($author_id, $following)) continue;

            // 发送系统通知
            $msg_data = array(
            'date' => current_time('mysql'),
            'from' => serialize(array(0)), // 系统发送
            'count' => 1,
            'to' => $follower->ID,
            'msg' => sprintf(
               '您关注的作者 %s %s了文章《<a href="%s">%s</a>》,快来看看吧~', 
                get_the_author_meta('display_name', $author_id),
                $is_new ? '发布' : '更新',
                get_permalink($post->ID),
                $post->post_title
            ),
            'type' => get_permalink($post->ID),
            'type_text' => $is_new ? '新文章通知' : '文章更新通知',
            'post_id' => $post->ID,
            'read' => 0
            );

            $wpdb->insert($table_name, $msg_data);
            // 更新未读消息计数
            delete_user_meta($follower->ID, 'b2_user_unread_msg');

            // 发送邮件通知
            $subject = get_bloginfo('name') . ': ' . ($is_new ? '新文章通知' : '文章更新通知');
            $message = '<!DOCTYPE html>
            <html>
            <head>
                <meta charset="UTF-8">
            </head>
            <body style="margin: 0; padding: 20px; background-color: #f5f5f5; font-family: Arial, sans-serif;">
                <div style="max-width: 600px; margin: 0 auto; background-color: #ffffff; padding: 20px; border-radius: 5px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
                    <h1 style="color: #333; margin-bottom: 20px; text-align: center;">' . 
                    ($is_new ? '新文章通知' : '文章更新通知') . '</h1>
                    <div style="border-left: 4px solid #0073aa; padding-left: 15px; margin: 20px 0;">
                        <h2 style="color: #333; margin: 0 0 10px 0;">' . $post->post_title . '</h2>
                        <p style="color: #666; line-height: 1.6; margin: 10px 0;">
                            您关注的作者' . get_the_author_meta('display_name', $author_id) . 
                            ($is_new ? '新文章通知' : '文章更新通知') . ',快来看看吧!
                        </p>
                    </div>
                    <div style="text-align: center; margin-top: 30px;">
                        <a href="'%20.%20get_permalink($post->ID)%20.%20'" 
                           style="display: inline-block; padding: 10px 20px; background-color: #0073aa; color: #fff; text-decoration: none; border-radius: 3px;">
                            阅读文章
                        </a>
                    </div>
                    <div style="margin-top: 30px; padding-top: 20px; border-top: 1px solid #eee; text-align: center; color: #999; font-size: 12px;">
                        <p>此邮件由' . get_bloginfo('name') . '系统自动发送,请勿直接回复</p>
                    </div>
                </div>
            </body>
            </html>';

            $headers = array(
                'Content-Type: text/html; charset=UTF-8',
                'From: ' . get_bloginfo('name') . ' <' . get_bloginfo('admin_email') . '>'
            );

            wp_mail($follower->user_email, $subject, $message, $headers);
        }
    } catch(\Exception $e) {
        error_log('发送通知失败: ' . $e->getMessage());
    }
}

add_action('transition_post_status', 'notify_followers', 999, 3);

 

代码说明

主要功能模块

  1. 触发条件检查
    • 检查是否为文章发布状态
    • 排除自动保存和修订版本
    • 验证作者ID是否有效
  2. 粉丝数据获取
    • 通过WordPress数据库查询获取所有可能的粉丝
    • 解析每个用户的关注数据
    • 确认是否真实关注了文章作者
  3. 站内通知
    • 向b2_msg表插入通知记录
    • 更新用户未读消息计数
    • 包含文章标题和链接信息
  4. 邮件通知
    • 使用HTML模板构建美观的邮件内容
    • 包含文章标题、作者信息和阅读按钮
    • 设置正确的邮件头部信息

注意事项

  1. 确保您的WordPress邮件发送功能正常工作
  2. 代码使用try-catch进行错误处理,避免影响正常的文章发布流程
  3. 通知模板可以根据需要自定义样式
  4. 函数优先级设置为999,确保在其他操作之后执行

故障排查

如果通知功能不正常,请检查:

  1. WordPress邮件发送功能是否正常
  2. 数据库表结构是否正确
  3. 错误日志中是否有相关错误信息
  4. 用户关注数据是否正确保存

希望这个教程能帮助您顺利实现粉丝通知功能。如果您在实施过程中遇到任何问题,欢迎留言讨论。

给TA打赏
共{{data.count}}人
人已打赏
网站教程

WordPress主题添加自定义文章类型register_post_type和分类

2021-9-2 22:30:32

网站教程美化教程

嘀嗒兔子主题-文章简码功能

2024-12-16 23:09:06

也想出现在这里?联系我们
嘀嗒兔
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索