WordPress 评论获奖功能

REWARD_END_TIME) {
return; // 如果不在活动期间,退出函数
}

if ($comment_approved == 1) { // 仅在评论通过审核后执行
$comment = get_comment($comment_ID);
$user_id = $comment->user_id;

// 仅针对注册用户且为父级评论
if ($user_id && $comment->comment_parent == 0) {
// 检查用户在当前活动期间是否已经获奖
$has_won = get_user_meta($user_id, REWARD_META_KEY, true);

if (!$has_won) {
// 设置中奖概率(例如 10% 概率)
$win_chance = 10;
if (mt_rand(1, 100) <= $win_chance) { // 标记用户在当前活动期间已中奖 update_user_meta($user_id, REWARD_META_KEY, true); // 将评论标记为获奖评论 update_comment_meta($comment_ID, 'is_winning_comment', true); // 获取用户信息 $user_info = get_userdata($user_id); $user_email = $user_info->user_email;
$user_name = $user_info->display_name;

// 发送获奖通知邮件
$subject = "恭喜您!您在评论中获得了奖励";
$message = "亲爱的 {$user_name},\n\n恭喜您!您的评论获得了奖励!\n\n感谢您参与我们的讨论,我们期待您的更多互动。\n\n最佳祝福,\n您的网站团队";

// 发送邮件
wp_mail($user_email, $subject, $message);
}
}
}
}
}

// Highlight the winning comment in the front end
add_filter('comment_class', 'highlight_winning_comment', 10, 5);
function highlight_winning_comment($classes, $class, $comment_ID, $comment, $post_ID) {
if (get_comment_meta($comment_ID, 'is_winning_comment', true)) {
$classes[] = 'winning-comment';
}
return $classes;
}

// Add custom styles for the winning comment
add_action('wp_head', 'winning_comment_custom_styles');
function winning_comment_custom_styles() {
echo '

';
}
?>

© 2025 杂记本,想到什么写点什么...