详细教程:为网站打造会员专属社区与论坛互动系统,通过WordPress代码二次开发实现常用互联网小工具功能
引言:为什么需要会员专属社区与论坛系统?
在当今互联网环境中,用户参与度和社区粘性已成为网站成功的关键因素。一个功能完善的会员专属社区与论坛系统不仅能提升用户体验,还能有效增加用户留存时间、促进内容生成和增强品牌忠诚度。对于内容创作者、在线教育平台、企业官网或任何希望建立稳定用户群体的网站来说,会员社区都是不可或缺的组成部分。
WordPress作为全球最流行的内容管理系统,拥有强大的扩展性和灵活性。通过代码二次开发,我们可以在WordPress平台上构建出功能丰富、高度定制化的会员社区系统,同时集成各种实用的互联网小工具,为用户提供全方位的互动体验。
本教程将详细指导您如何从零开始,通过WordPress代码二次开发,打造一个功能完整的会员专属社区与论坛互动系统,并集成常用互联网小工具功能。
第一部分:前期准备与环境搭建
1.1 选择合适的WordPress主题
在开始开发之前,选择一个适合社区建设的WordPress主题至关重要。建议选择以下类型的主题:
- 响应式设计:确保社区在所有设备上都能良好显示
- 轻量级框架:避免过多不必要的功能影响性能
- 良好的扩展性:支持自定义开发和无冲突的代码结构
- 社区友好型:内置或兼容论坛、用户资料等社区功能
推荐主题:Astra、GeneratePress、OceanWP等轻量级多功能主题。
1.2 必备插件安装与配置
虽然我们将主要通过代码开发实现功能,但一些基础插件可以节省开发时间:
- BuddyPress:提供基础的社交网络功能框架
- bbPress:轻量级论坛系统,与WordPress完美集成
- MemberPress或WooCommerce Memberships:会员管理系统
- Advanced Custom Fields:自定义字段管理,便于扩展用户资料
- User Role Editor:用户角色和权限管理
1.3 开发环境配置
为了安全地进行代码开发,建议配置本地开发环境:
- 安装Local by Flywheel或XAMPP作为本地服务器环境
-
设置调试模式:在wp-config.php中添加以下代码:
define('WP_DEBUG', true); define('WP_DEBUG_LOG', true); define('WP_DEBUG_DISPLAY', false); - 安装代码编辑器:VS Code、Sublime Text或PHPStorm
- 配置版本控制系统(Git)以便代码管理
第二部分:会员系统核心功能开发
2.1 自定义用户角色与权限系统
WordPress默认的用户角色可能无法满足社区需求,我们需要创建更精细的角色体系:
// 在主题的functions.php或自定义插件中添加
function create_community_user_roles() {
// 添加青铜会员角色
add_role('bronze_member', '青铜会员', array(
'read' => true,
'edit_posts' => true,
'delete_posts' => false,
'publish_posts' => false,
'upload_files' => true,
'participate_in_forum' => true,
'access_basic_content' => true
));
// 添加白银会员角色
add_role('silver_member', '白银会员', array(
'read' => true,
'edit_posts' => true,
'delete_posts' => true,
'publish_posts' => true,
'upload_files' => true,
'participate_in_forum' => true,
'access_premium_content' => true,
'create_topics' => true
));
// 添加黄金会员角色
add_role('gold_member', '黄金会员', array(
'read' => true,
'edit_posts' => true,
'edit_others_posts' => true,
'delete_posts' => true,
'delete_others_posts' => false,
'publish_posts' => true,
'upload_files' => true,
'participate_in_forum' => true,
'access_all_content' => true,
'create_topics' => true,
'moderate_comments' => true,
'access_exclusive_areas' => true
));
}
add_action('init', 'create_community_user_roles');
2.2 扩展用户资料字段
使用Advanced Custom Fields插件或自定义代码扩展用户资料:
// 自定义用户资料字段
function add_custom_user_profile_fields($user) {
if(!current_user_can('edit_user', $user->ID)) {
return false;
}
?>
<h3>社区资料</h3>
<table class="form-table">
<tr>
<th><label for="user_location">所在地</label></th>
<td>
<input type="text" name="user_location" id="user_location" value="<?php echo esc_attr(get_the_author_meta('user_location', $user->ID)); ?>" class="regular-text" />
</td>
</tr>
<tr>
<th><label for="user_occupation">职业</label></th>
<td>
<input type="text" name="user_occupation" id="user_occupation" value="<?php echo esc_attr(get_the_author_meta('user_occupation', $user->ID)); ?>" class="regular-text" />
</td>
</tr>
<tr>
<th><label for="user_interests">兴趣标签</label></th>
<td>
<input type="text" name="user_interests" id="user_interests" value="<?php echo esc_attr(get_the_author_meta('user_interests', $user->ID)); ?>" class="regular-text" />
<p class="description">用逗号分隔多个兴趣标签</p>
</td>
</tr>
<tr>
<th><label for="user_signature">个人签名</label></th>
<td>
<textarea name="user_signature" id="user_signature" rows="3" cols="30"><?php echo esc_textarea(get_the_author_meta('user_signature', $user->ID)); ?></textarea>
</td>
</tr>
</table>
<?php
}
add_action('show_user_profile', 'add_custom_user_profile_fields');
add_action('edit_user_profile', 'add_custom_user_profile_fields');
// 保存自定义用户字段
function save_custom_user_profile_fields($user_id) {
if(!current_user_can('edit_user', $user_id)) {
return false;
}
update_user_meta($user_id, 'user_location', sanitize_text_field($_POST['user_location']));
update_user_meta($user_id, 'user_occupation', sanitize_text_field($_POST['user_occupation']));
update_user_meta($user_id, 'user_interests', sanitize_text_field($_POST['user_interests']));
update_user_meta($user_id, 'user_signature', sanitize_textarea_field($_POST['user_signature']));
}
add_action('personal_options_update', 'save_custom_user_profile_fields');
add_action('edit_user_profile_update', 'save_custom_user_profile_fields');
2.3 会员等级与权限控制
实现基于会员等级的权限控制系统:
// 检查用户是否有特定权限
function community_user_can($user_id, $capability) {
$user = get_user_by('id', $user_id);
if(!$user) {
return false;
}
// 获取用户角色
$user_roles = $user->roles;
// 定义各角色权限映射
$role_capabilities = array(
'bronze_member' => array(
'access_basic_content',
'participate_in_forum',
'view_profiles'
),
'silver_member' => array(
'access_basic_content',
'access_premium_content',
'participate_in_forum',
'create_topics',
'view_profiles',
'send_private_messages'
),
'gold_member' => array(
'access_basic_content',
'access_premium_content',
'access_all_content',
'participate_in_forum',
'create_topics',
'moderate_comments',
'view_profiles',
'send_private_messages',
'access_exclusive_areas',
'download_resources'
),
'administrator' => array('all') // 管理员拥有所有权限
);
// 检查用户是否有权限
foreach($user_roles as $role) {
if(array_key_exists($role, $role_capabilities)) {
if(in_array('all', $role_capabilities[$role]) || in_array($capability, $role_capabilities[$role])) {
return true;
}
}
}
return false;
}
// 使用示例:在模板中检查权限
function display_premium_content() {
$user_id = get_current_user_id();
if(community_user_can($user_id, 'access_premium_content')) {
// 显示高级内容
echo '<div class="premium-content">这里是高级会员专属内容</div>';
} else {
// 显示升级提示
echo '<div class="upgrade-prompt">升级为高级会员以查看此内容</div>';
}
}
第三部分:论坛系统开发与集成
3.1 基于bbPress的论坛定制
bbPress是一个轻量级论坛插件,我们可以通过代码进行深度定制:
// 自定义论坛主题模板
function custom_bbpress_templates() {
// 重写bbPress模板路径
return get_stylesheet_directory() . '/bbpress/';
}
add_filter('bbp_get_template_stack', 'custom_bbpress_templates', 10, 1);
// 创建自定义论坛分类
function create_custom_forum_structure() {
// 技术讨论区
$tech_forum_id = wp_insert_post(array(
'post_title' => '技术讨论区',
'post_content' => '关于网站开发、编程技术的讨论',
'post_status' => 'publish',
'post_type' => 'forum',
'post_author' => 1
));
// 在技术讨论区下创建子论坛
if($tech_forum_id) {
wp_insert_post(array(
'post_title' => '前端开发',
'post_content' => 'HTML、CSS、JavaScript等前端技术讨论',
'post_status' => 'publish',
'post_type' => 'forum',
'post_parent' => $tech_forum_id,
'post_author' => 1
));
wp_insert_post(array(
'post_title' => '后端开发',
'post_content' => 'PHP、Python、数据库等后端技术讨论',
'post_status' => 'publish',
'post_type' => 'forum',
'post_parent' => $tech_forum_id,
'post_author' => 1
));
}
// 创建其他论坛分类...
}
// 注释掉这行,除非您确实需要创建论坛结构
// add_action('init', 'create_custom_forum_structure');
3.2 论坛权限与访问控制
// 限制特定论坛的访问权限
function restrict_forum_access($capabilities, $forum_id) {
// 获取当前用户
$user_id = get_current_user_id();
// 获取论坛元数据
$forum_access_level = get_post_meta($forum_id, '_forum_access_level', true);
// 如果没有设置访问级别,使用默认权限
if(empty($forum_access_level)) {
return $capabilities;
}
// 检查用户是否有访问权限
if(!community_user_can($user_id, 'access_' . $forum_access_level . '_content')) {
// 如果没有权限,移除阅读能力
$capabilities['read_others_topics'] = false;
$capabilities['view_forum'] = false;
}
return $capabilities;
}
add_filter('bbp_get_forum_caps', 'restrict_forum_access', 10, 2);
// 在论坛编辑页面添加访问级别选择
function add_forum_access_level_metabox() {
add_meta_box(
'forum_access_level',
'论坛访问级别',
'render_forum_access_level_metabox',
'forum',
'side',
'high'
);
}
add_action('add_meta_boxes', 'add_forum_access_level_metabox');
function render_forum_access_level_metabox($post) {
wp_nonce_field('save_forum_access_level', 'forum_access_level_nonce');
$current_value = get_post_meta($post->ID, '_forum_access_level', true);
$options = array(
'basic' => '基础会员',
'premium' => '高级会员',
'all' => '所有会员'
);
echo '<select name="forum_access_level" id="forum_access_level">';
foreach($options as $value => $label) {
echo '<option value="' . esc_attr($value) . '" ' . selected($current_value, $value, false) . '>' . esc_html($label) . '</option>';
}
echo '</select>';
echo '<p class="description">设置可以访问此论坛的最低会员级别</p>';
}
function save_forum_access_level($post_id) {
if(!isset($_POST['forum_access_level_nonce']) || !wp_verify_nonce($_POST['forum_access_level_nonce'], 'save_forum_access_level')) {
return;
}
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
if(!current_user_can('edit_post', $post_id)) {
return;
}
if(isset($_POST['forum_access_level'])) {
update_post_meta($post_id, '_forum_access_level', sanitize_text_field($_POST['forum_access_level']));
}
}
add_action('save_post_forum', 'save_forum_access_level');
3.3 论坛功能增强
// 添加点赞功能
function add_topic_like_system() {
// 在前端添加点赞按钮
add_action('bbp_theme_after_topic_content', 'display_like_button');
// 处理AJAX点赞请求
add_action('wp_ajax_topic_like', 'handle_topic_like');
add_action('wp_ajax_nopriv_topic_like', 'require_login_for_like');
}
add_action('init', 'add_topic_like_system');
function display_like_button() {
if(!is_singular('topic')) {
return;
}
$topic_id = get_the_ID();
$user_id = get_current_user_id();
$like_count = get_post_meta($topic_id, '_topic_likes', true) ?: 0;
$user_liked = $user_id ? get_user_meta($user_id, '_liked_topic_' . $topic_id, true) : false;
?>
<div class="topic-like-system">
<button class="like-button <?php echo $user_liked ? 'liked' : ''; ?>" data-topic-id="<?php echo $topic_id; ?>">
<span class="like-icon">👍</span>
<span class="like-count"><?php echo $like_count; ?></span>
</button>
</div>
<script>
jQuery(document).ready(function($) {
$('.like-button').on('click', function() {
var button = $(this);
var topicId = button.data('topic-id');
$.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: 'POST',
data: {
action: 'topic_like',
topic_id: topicId,
nonce: '<?php echo wp_create_nonce('topic_like_nonce'); ?>'
},
success: function(response) {
if(response.success) {
button.toggleClass('liked');
button.find('.like-count').text(response.data.like_count);
} else {
alert(response.data.message);
}
}
});
});
});
</script>
<?php
}
function handle_topic_like() {
// 验证nonce
if(!wp_verify_nonce($_POST['nonce'], 'topic_like_nonce')) {
wp_die('安全验证失败');
}
$topic_id = intval($_POST['topic_id']);
$user_id = get_current_user_id();
if(!$user_id) {
wp_send_json_error(array('message' => '请先登录'));
}
$user_liked = get_user_meta($user_id, '_liked_topic_' . $topic_id, true);
$like_count = get_post_meta($topic_id, '_topic_likes', true) ?: 0;
if($user_liked) {
// 取消点赞
delete_user_meta($user_id, '_liked_topic_' . $topic_id);
$like_count = max(0, $like_count - 1);
update_post_meta($topic_id, '_topic_likes', $like_count);
} else {
// 点赞
update_user_meta($user_id, '_liked_topic_' . $topic_id, true);
$like_count++;
update_post_meta($topic_id, '_topic_likes', $like_count);
}
wp_send_json_success(array('like_count' => $like_count));
}
function require_login_for_like() {
wp_send_json_error(array('message' => '请先登录'));
}
第四部分:集成常用互联网小工具
4.1 实时通知系统
// 创建通知系统
class Community_Notification_System {
private static $instance = null;
public static function get_instance() {
if(null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
private function __construct() {
4.1 实时通知系统(续)
add_action('wp_ajax_get_notifications', array($this, 'ajax_get_notifications'));
add_action('wp_ajax_mark_notification_read', array($this, 'ajax_mark_notification_read'));
add_action('wp_enqueue_scripts', array($this, 'enqueue_notification_scripts'));
add_action('admin_bar_menu', array($this, 'add_notification_to_admin_bar'), 100);
}
// 创建新通知
public static function create_notification($user_id, $type, $message, $link = '', $sender_id = 0) {
$notifications = get_user_meta($user_id, '_community_notifications', true);
if(!is_array($notifications)) {
$notifications = array();
}
$notification = array(
'id' => uniqid('notif_'),
'type' => $type,
'message' => $message,
'link' => $link,
'sender_id' => $sender_id,
'timestamp' => current_time('timestamp'),
'read' => false
);
array_unshift($notifications, $notification);
// 只保留最近的50条通知
if(count($notifications) > 50) {
$notifications = array_slice($notifications, 0, 50);
}
update_user_meta($user_id, '_community_notifications', $notifications);
// 更新未读计数
self::update_unread_count($user_id);
return $notification['id'];
}
// 更新未读计数
private static function update_unread_count($user_id) {
$notifications = get_user_meta($user_id, '_community_notifications', true);
$unread_count = 0;
if(is_array($notifications)) {
foreach($notifications as $notification) {
if(!$notification['read']) {
$unread_count++;
}
}
}
update_user_meta($user_id, '_community_unread_notifications', $unread_count);
return $unread_count;
}
// 获取用户通知
public static function get_user_notifications($user_id, $limit = 20) {
$notifications = get_user_meta($user_id, '_community_notifications', true);
if(!is_array($notifications)) {
return array();
}
if($limit > 0) {
$notifications = array_slice($notifications, 0, $limit);
}
return $notifications;
}
// 标记通知为已读
public static function mark_as_read($user_id, $notification_id = 'all') {
$notifications = get_user_meta($user_id, '_community_notifications', true);
if(!is_array($notifications)) {
return false;
}
$updated = false;
foreach($notifications as &$notification) {
if($notification_id === 'all' || $notification['id'] === $notification_id) {
if(!$notification['read']) {
$notification['read'] = true;
$updated = true;
}
}
}
if($updated) {
update_user_meta($user_id, '_community_notifications', $notifications);
self::update_unread_count($user_id);
}
return $updated;
}
// AJAX获取通知
public function ajax_get_notifications() {
if(!is_user_logged_in()) {
wp_die('请先登录');
}
$user_id = get_current_user_id();
$notifications = self::get_user_notifications($user_id, 10);
wp_send_json_success(array(
'notifications' => $notifications,
'unread_count' => get_user_meta($user_id, '_community_unread_notifications', true) ?: 0
));
}
// AJAX标记通知为已读
public function ajax_mark_notification_read() {
if(!is_user_logged_in()) {
wp_die('请先登录');
}
$user_id = get_current_user_id();
$notification_id = isset($_POST['notification_id']) ? sanitize_text_field($_POST['notification_id']) : 'all';
self::mark_as_read($user_id, $notification_id);
wp_send_json_success();
}
// 加载通知脚本
public function enqueue_notification_scripts() {
if(!is_user_logged_in()) {
return;
}
wp_enqueue_script('community-notifications', get_template_directory_uri() . '/js/notifications.js', array('jquery'), '1.0', true);
wp_localize_script('community-notifications', 'communityNotifications', array(
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('notification_nonce'),
'poll_interval' => 30000 // 30秒轮询一次
));
}
// 在管理栏添加通知
public function add_notification_to_admin_bar($admin_bar) {
if(!is_user_logged_in()) {
return;
}
$user_id = get_current_user_id();
$unread_count = get_user_meta($user_id, '_community_unread_notifications', true) ?: 0;
$admin_bar->add_node(array(
'id' => 'community-notifications',
'title' => '通知' . ($unread_count > 0 ? ' <span class="notification-count">' . $unread_count . '</span>' : ''),
'href' => '#',
'meta' => array(
'class' => 'community-notifications-menu',
'html' => $this->get_notification_dropdown_html()
)
));
}
// 获取通知下拉菜单HTML
private function get_notification_dropdown_html() {
if(!is_user_logged_in()) {
return '';
}
$user_id = get_current_user_id();
$notifications = self::get_user_notifications($user_id, 5);
ob_start();
?>
<div class="notification-dropdown" style="display: none;">
<div class="notification-header">
<h3>通知</h3>
<?php if(!empty($notifications)): ?>
<a href="#" class="mark-all-read">全部标记为已读</a>
<?php endif; ?>
</div>
<div class="notification-list">
<?php if(empty($notifications)): ?>
<div class="notification-item no-notifications">
暂无新通知
</div>
<?php else: ?>
<?php foreach($notifications as $notification): ?>
<div class="notification-item <?php echo $notification['read'] ? 'read' : 'unread'; ?>" data-notification-id="<?php echo esc_attr($notification['id']); ?>">
<div class="notification-content">
<p><?php echo esc_html($notification['message']); ?></p>
<span class="notification-time"><?php echo human_time_diff($notification['timestamp'], current_time('timestamp')) . '前'; ?></span>
</div>
<?php if(!empty($notification['link'])): ?>
<a href="<?php echo esc_url($notification['link']); ?>" class="notification-link"></a>
<?php endif; ?>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
<div class="notification-footer">
<a href="<?php echo esc_url(home_url('/notifications/')); ?>">查看所有通知</a>
</div>
</div>
<?php
return ob_get_clean();
}
}
// 初始化通知系统
Community_Notification_System::get_instance();
// 创建通知触发器
function trigger_community_notifications() {
// 当用户收到私信时
add_action('bp_messages_message_sent', function($message) {
$recipients = $message->get_recipients();
foreach($recipients as $recipient) {
if($recipient->user_id != $message->sender_id) {
$sender = get_userdata($message->sender_id);
Community_Notification_System::create_notification(
$recipient->user_id,
'message',
$sender->display_name . ' 给您发送了一条私信',
bp_core_get_user_domain($message->sender_id) . 'messages/view/' . $message->thread_id . '/',
$message->sender_id
);
}
}
});
// 当用户的话题收到回复时
add_action('bbp_new_reply', function($reply_id, $topic_id, $forum_id, $anonymous_data, $reply_author) {
$topic_author = get_post_field('post_author', $topic_id);
if($topic_author != $reply_author) {
$reply_author_name = get_the_author_meta('display_name', $reply_author);
Community_Notification_System::create_notification(
$topic_author,
'reply',
$reply_author_name . ' 回复了您的话题',
get_permalink($reply_id),
$reply_author
);
}
}, 10, 5);
}
add_action('init', 'trigger_community_notifications');
4.2 私信系统
// 增强私信功能
class Enhanced_Private_Messaging {
public function __construct() {
// 检查BuddyPress是否存在
if(!function_exists('bp_is_active')) {
return;
}
// 添加消息类型
add_filter('bp_messages_allowed_tags', array($this, 'allow_additional_message_tags'));
// 添加消息附件功能
add_action('bp_after_messages_compose_content', array($this, 'add_attachment_field'));
add_action('messages_message_sent', array($this, 'handle_message_attachment'), 10, 1);
// 添加消息搜索
add_action('bp_messages_directory_search_form', array($this, 'add_message_search'));
// 添加消息分类标签
add_action('bp_before_message_compose_input', array($this, 'add_message_labels'));
}
// 允许更多HTML标签
public function allow_additional_message_tags($allowed_tags) {
$allowed_tags['img'] = array(
'src' => true,
'alt' => true,
'width' => true,
'height' => true,
'class' => true
);
$allowed_tags['a']['target'] = true;
$allowed_tags['code'] = array();
$allowed_tags['pre'] = array();
return $allowed_tags;
}
// 添加附件字段
public function add_attachment_field() {
if(!community_user_can(get_current_user_id(), 'send_private_messages')) {
return;
}
?>
<div class="message-attachment-field">
<label for="message-attachment">添加附件</label>
<input type="file" name="message_attachment" id="message-attachment" accept=".jpg,.jpeg,.png,.gif,.pdf,.doc,.docx,.txt">
<p class="description">支持图片、文档文件,最大2MB</p>
</div>
<script>
jQuery(document).ready(function($) {
$('#send_message_form').on('submit', function(e) {
var fileInput = $('#message-attachment')[0];
if(fileInput.files.length > 0) {
var file = fileInput.files[0];
var maxSize = 2 * 1024 * 1024; // 2MB
if(file.size > maxSize) {
alert('文件大小不能超过2MB');
e.preventDefault();
return false;
}
// 创建FormData对象
var formData = new FormData(this);
formData.append('message_attachment', file);
// 使用AJAX提交表单
e.preventDefault();
$.ajax({
url: $(this).attr('action'),
type: 'POST',
data: formData,
processData: false,
contentType: false,
success: function(response) {
// 处理成功响应
window.location.reload();
}
});
}
});
});
</script>
<?php
}
// 处理消息附件
public function handle_message_attachment($message) {
if(!empty($_FILES['message_attachment']['name'])) {
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
$upload = wp_handle_upload($_FILES['message_attachment'], array(
'test_form' => false,
'action' => 'bp_message_attachment'
));
if(!isset($upload['error'])) {
// 保存附件信息到消息元数据
bp_messages_update_meta($message->id, 'attachment_url', $upload['url']);
bp_messages_update_meta($message->id, 'attachment_type', $_FILES['message_attachment']['type']);
bp_messages_update_meta($message->id, 'attachment_name', $_FILES['message_attachment']['name']);
}
}
}
// 显示消息附件
public static function display_message_attachment($message_id) {
$attachment_url = bp_messages_get_meta($message_id, 'attachment_url', true);
if($attachment_url) {
$attachment_type = bp_messages_get_meta($message_id, 'attachment_type', true);
$attachment_name = bp_messages_get_meta($message_id, 'attachment_name', true);
if(strpos($attachment_type, 'image/') === 0) {
echo '<div class="message-attachment image-attachment">';
echo '<a href="' . esc_url($attachment_url) . '" target="_blank">';
echo '<img src="' . esc_url($attachment_url) . '" alt="' . esc_attr($attachment_name) . '" style="max-width: 300px;">';
echo '</a>';
echo '</div>';
} else {
echo '<div class="message-attachment file-attachment">';
echo '<a href="' . esc_url($attachment_url) . '" target="_blank" class="attachment-link">';
echo '<span class="attachment-icon">📎</span>';
echo '<span class="attachment-name">' . esc_html($attachment_name) . '</span>';
echo '</a>';
echo '</div>';
}
}
}
// 添加消息搜索
public function add_message_search() {
?>
<div class="message-search-form">
<form action="<?php echo bp_get_messages_directory_permalink(); ?>" method="get">
<input type="text" name="s" placeholder="搜索消息..." value="<?php echo isset($_GET['s']) ? esc_attr($_GET['s']) : ''; ?>">
<button type="submit">搜索</button>
</form>
</div>
<?php
}
// 添加消息标签
public function add_message_labels() {
?>
<div class="message-labels">
<label>消息标签:</label>
<select name="message_label" id="message-label">
<option value="">无标签</option>
<option value="important">重要</option>
<option value="work">工作</option>
<option value="personal">个人</option>
<option value="question">问题</option>
</select>
</div>
<?php
}
}
// 初始化增强私信系统
new Enhanced_Private_Messaging();
// 在消息显示时添加附件
add_action('bp_after_message_content', function() {
global $messages_template;
if(isset($messages_template->message->id)) {
Enhanced_Private_Messaging::display_message_attachment($messages_template->message->id);
}
});
4.3 积分与成就系统
// 积分与成就系统
class Points_and_Achievements_System {
private $achievements = array();
public function __construct() {
$this->define_achievements();
add_action('init', array($this, 'register_points_post_type'));
add_action('user_register', array($this, 'award_registration_points'));
add_action('bbp_new_topic', array($this, 'award_topic_creation_points'), 10, 4);
add_action('bbp_new_reply', array($this, 'award_reply_points'), 10, 5);
add_action('bp_activity_add', array($this, 'check_activity_achievements'));
add_action('wp_ajax_get_user_points', array($this, 'ajax_get_user_points'));
add_action('wp_enqueue_scripts', array($this, 'enqueue_points_scripts'));
// 添加积分显示到用户资料
add_action('bp_profile_header_meta', array($this, 'display_user_points_in_profile'));
}
// 定义成就
private function define_achievements() {
$this->achievements = array(
'first_post' => array(
'name' => '初来乍到',
'description' => '发表第一个帖子',
'points' => 100,
'badge' => '🥇'
),
'active_member' => array(
'name' => '活跃会员',
'description' => '连续7天登录',
'points' => 200,
'badge' => '🔥'
),
'helpful_member' => array(
'name' => '热心助人',
'description' => '获得10个感谢',
'points' => 300,
'badge' => '❤️'
),
'expert' => array(
'name' => '社区专家',
'description' => '发表50个高质量回复',
'points' => 500,
'badge' => '👑'
),
'veteran' => array(
