B2主题美化 用户发布或更新文章站内邮件通知

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

 

在社交化的博客平台中,及时通知粉丝作者发布或更新文章是一个提升用户互动和留存率的重要功能。本教程将详细介绍如何在7b2主题中实现这一功能,确保您的粉丝能够在第一时间收到文章更新的通知。

 

功能概述

 

- **发布新文章**:当作者发布新文章时,粉丝将收到站内通知和邮件通知。

- **更新文章**:当作者更新已发布的文章时,粉丝同样会收到通知。

- **通知内容**:通知中包含文章标题和链接,方便粉丝直接查看。

实现步骤

 

1. 创建子主题(可选)

 

如果您尚未创建子主题,建议先创建一个子主题,以避免直接修改主题文件导致更新时丢失自定义代码。

 

2. 添加代码到 `functions.php`

 

将以下代码添加到7b2子主题的 `functions.php` 文件中:

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 = '
            
            
                
            
            
                
                    
' . 
                    ($is_new ? '新文章通知' : '文章更新通知') . '
                    
                        
' . $post->post_title . '
                        
                            您关注的作者' . get_the_author_meta('display_name', $author_id) . 
                            ($is_new ? '新文章通知' : '文章更新通知') . ',快来看看吧!
                        
                    

                    
                        ID) . '" 
                           style="display: inline-block; padding: 10px 20px; background-color: #0073aa; color: #fff; text-decoration: none; border-radius: 3px;">
                            阅读文章
                        
                    
                    
                        
此邮件由' . get_bloginfo('name') . '系统自动发送,请勿直接回复
                    
                
            
            ';
            $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);

 

 

阅读剩余
THE END