适用于WordPress网站7B2主题美化修改教程:为用户提供实时的内容更新通知,当用户关注的作者发布或更新文章/话题时,系统自动通过站内消息和邮件通知关注者。支持文章和话题。触发条件 作者首次发布文章/话题(状态从草稿/待审核 → 发布) 作者更新已发布的内容(修改后重新发布)。
1.B2主题修改代码
代码来自网络,我都找不到谁是原作者了,我改了下,新增了一个话题通知
将下面的代码添加到function.php
文件底部即可
1.1 仅文章通知代码
仅发布文章才通知
//发布文章通知用户 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="' . get_permalink($post->ID) . '" 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.2 文章话题通知
文章和话题发布均通知
2.相关文章
2.1 定制开发接单
B2主题可扩展的东西很多,老白会结合自己的使用情况发布相关教程或者插件
如有个性化定制以及功能开发可联系老白微-信:x c b t m w(防爬虫间隔符)
2.2 建站经验
B2主题通用美化:https://www.dzw6.com/tag/b2-theme-beautify
B2主题圈子美化:https://www.dzw6.com/tag/b2_theme_circle
WordPress建站经验:https://www.dzw6.com/share/wordpress
可以,看看先!
不错,学习一下