本文是《免费(共164篇)》专题的第 163 篇。阅读本文前,您可以先阅读前面的一些文章:
b2主题实现用户发布或更新文章后,通知粉丝
在社交化的博客平台中,当作者发布或更新文章时,及时通知其粉丝是一个非常实用的功能。本教程将指导您如何在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了文章《%s》,快来看看吧~', 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 = ''; $headers = array( 'Content-Type: text/html; charset=UTF-8', 'From: ' . get_bloginfo('name') . ' ' ); wp_mail($follower->user_email, $subject, $message, $headers); } } catch(\Exception $e) { error_log('发送通知失败: ' . $e->getMessage()); } } add_action('transition_post_status', 'notify_followers', 999, 3);' . ($is_new ? '新文章通知' : '文章更新通知') . '
' . $post->post_title . '
您关注的作者' . get_the_author_meta('display_name', $author_id) . ($is_new ? '新文章通知' : '文章更新通知') . ',快来看看吧!
此邮件由' . get_bloginfo('name') . '系统自动发送,请勿直接回复
以上代码会使用邮件通知用户,会让用户感觉很烦,以下是去除邮件通知功能
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了文章《%s》,快来看看吧~', 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'); } } catch(\Exception $e) { error_log('发送通知失败: ' . $e->getMessage()); } } add_action('transition_post_status', 'notify_followers', 999, 3);
代码说明
主要功能模块
触发条件检查
检查是否为文章发布状态
排除自动保存和修订版本
验证作者ID是否有效
粉丝数据获取
通过WordPress数据库查询获取所有可能的粉丝
解析每个用户的关注数据
确认是否真实关注了文章作者
站内通知
向b2_msg表插入通知记录
更新用户未读消息计数
包含文章标题和链接信息
邮件通知
使用HTML模板构建美观的邮件内容
包含文章标题、作者信息和阅读按钮
设置正确的邮件头部信息
注意事项
确保您的WordPress邮件发送功能正常工作
代码使用try-catch进行错误处理,避免影响正常的文章发布流程
通知模板可以根据需要自定义样式
函数优先级设置为999,确保在其他操作之后执行
故障排查
如果通知功能不正常,请检查:
WordPress邮件发送功能是否正常
数据库表结构是否正确
错误日志中是否有相关错误信息
用户关注数据是否正确保存
希望这个教程能帮助您顺利实现粉丝通知功能。如果您在实施过程中遇到任何问题,欢迎留言讨论。
谢谢你的分享,我从中学到了很多!