返回列表 发布新帖
查看: 60|回复: 0

[代码技巧] 子比主题 – 新版私密评论功能

[复制链接]
SunJu_FaceMall
社区贡献

315

主题

190

回帖

1万

积分

等级头衔
Icon组别 : 管理员
Icon等级 :

积分成就
   钻石 : 524 颗
   贡献 : 1779 点
   金币 : 12 枚
Icon在线时间 : 1295 小时
Icon注册时间 : 2024-11-22
Icon最后登录 : 2025-11-11

荣誉勋章

会员LV.1会员LV.2会员LV.3会员LV.4会员LV.5会员LV.6会员LV.7会员LV.8会员LV.9会员LV.10

风云·优秀版主

飞流名人堂成员

1

实名认证 手机认证 vip vip-year FLLTCN发表于 2025-10-10 18:13:59 | 查看全部 |阅读模式 浙江金华

资源无需等待,交易就趁现在,全面资源整合网络大咖云集,让你轻松玩转互联网!

您需要 登录 才可以下载或查看,没有账号?立即注册

×
这是一款子比主题新私密评论功能,这款是适配了子比的样式,在此之前腾飞博客给大家分享过改文件的代码,但是那些太麻烦了,今天腾飞博客给大家分享一款无需修改子比文件即可实现的,喜欢的自行部署吧!

tengfei_down - 2025-10-10T181150.748.webp
tengfei_down - 2025-10-10T181207.921.webp

2025/9/14修复
  • 修复当从私密设为不私密之后,未登录用户看不到评论
  • 添加版本二提交评论按钮样式(与旧版本一样的按钮)

代码部署




首先我写了两个版本,第一个是点该评论下面三个点有私密,第二个就是评论下面三个点也有而且再加上提交评论旁边也有一个私密按钮,这样用户很方便去加私密,推荐版本二的效果(本站正在使用的),不过要修改子比文件,很简单的!
版本一
tengfei_down (11).webp
定位:/wp-content/themes/zibll/func.php文件,没有这个文件自己创建一个,记得加上php头,要不然会报错,将下面的代码直接丢里面即可!
  1. // 腾飞博客修复私密功能
  2. function zib_private_message_hook($comment_content, $comment) {
  3.     $comment_ID = $comment->comment_ID;
  4.     $is_private = get_comment_meta($comment_ID, '_private', true);
  5.     $current_user = wp_get_current_user();
  6.     $html = '<div class="hidden-box" reply-show="true" reload-hash="#hidden-box-comment"><a class="hidden-text"><i class="fa fa-exclamation-circle mr10"></i>此评论为私密评论,仅双方可见.</a></div>';

  7.     if ($is_private) {
  8.         $is_comment_author = false;
  9.         $is_post_author = false;
  10.         $is_admin = current_user_can('manage_options');

  11.         if ($current_user && $current_user->ID) {
  12.             $is_comment_author = ($current_user->ID == $comment->user_id);
  13.             $is_post_author = ($current_user->ID == get_post_field('post_author', $comment->comment_post_ID));
  14.         }

  15.         if ($is_admin || $is_comment_author || $is_post_author) {
  16.             return '<div class="hidden-box show" id="hidden-box-comment"><div class="hidden-text">私密评论内容</div>' . $comment_content . '</div>';
  17.         }

  18.         return $html;
  19.     }
  20.    
  21.     return $comment_content;
  22. }
  23. add_filter('get_comment_text', 'zib_private_message_hook', 10, 2);

  24. function zib_comments_action_add_private($lists, $comment) {
  25.     $current_user = wp_get_current_user();
  26.     $user_id = get_current_user_id();

  27.     if (is_user_logged_in() && (is_super_admin($user_id) || $current_user->ID == $comment->user_id)) {

  28.         $comment_ID = $comment->comment_ID;
  29.         $is_private = get_comment_meta($comment_ID, '_private', true);
  30.         $private_text = empty($is_private) ? '设为私密' : '取消私密';
  31.         $action = empty($is_private) ? 'set_private' : 'del_private';
  32.         $private_but = '<a class="comment-private-link wp-ajax-submit" form-action="' . esc_attr($action) . '" form-data="' . esc_attr(json_encode(['id' => $comment_ID])) . '" href="javascript:;"><i class="fa fa-user-secret mr10" aria-hidden="true"></i>' . $private_text . '</a>';

  33.         $lists = '<li>' . $private_but . '</li>' . $lists;
  34.     }

  35.     return $lists;
  36. }
  37. add_filter('comments_action_lists', 'zib_comments_action_add_private', 99, 2);
  38. function zib_private_comment_action() {
  39.     $response = ['reload' => true];

  40.     if (!isset($_POST['action']) || !$_POST['action']) {
  41.         zib_send_json_error(['msg' => '无效的操作类型'] + $response);
  42.     }

  43.     if (!isset($_POST['id']) || !$_POST['id']) {
  44.         zib_send_json_error(['msg' => '无效的评论ID'] + $response);
  45.     }
  46.     $comment_id = intval($_POST['id']);
  47.     $action = sanitize_text_field($_POST['action']);
  48.     $comment = get_comment($comment_id);
  49.     $post_author_id = get_post_field('post_author', $comment->comment_post_ID);
  50.     $current_user = wp_get_current_user();
  51.     if (!$comment_id || !$current_user || !($current_user->ID == $comment->user_id || current_user_can('manage_options'))) {
  52.         zib_send_json_error(['msg' => '权限不足或无效的评论ID'] + $response);
  53.     }

  54.     if ($action === 'set_private') {
  55.         update_comment_meta($comment_id, '_private', 'true');
  56.         zib_send_json_success(['msg' => '评论已设为私密'] + $response);
  57.     } elseif ($action === 'del_private') {
  58.         delete_comment_meta($comment_id, '_private');
  59.         zib_send_json_success(['msg' => '评论已公开'] + $response);
  60.     } else {
  61.         zib_send_json_error(['msg' => '无效的操作类型'] + $response);
  62.     }
  63. }
  64. add_action('wp_ajax_set_private', 'zib_private_comment_action');
  65. add_action('wp_ajax_del_private', 'zib_private_comment_action');
复制代码

版本二
tengfei_down (12).webp
定位:/wp-content/themes/zibll/func.php文件,没有这个文件自己创建一个,记得加上php头,要不然会报错,将下面的代码直接丢里面即可!
  1. // 腾飞博客修复私密功能 - 版本二
  2. function zib_private_message_hook($comment_content, $comment) {
  3.     $comment_ID = $comment->comment_ID;
  4.     $is_private = get_comment_meta($comment_ID, '_private', true);
  5.     $current_user = wp_get_current_user();
  6.     $html = '<div class="hidden-box" reply-show="true" reload-hash="#hidden-box-comment"><a class="hidden-text"><i class="fa fa-exclamation-circle mr10"></i>此评论为私密评论,仅双方可见.</a></div>';

  7.     if ($is_private) {
  8.         $is_comment_author = false;
  9.         $is_post_author = false;
  10.         $is_admin = current_user_can('manage_options');

  11.         if ($current_user && $current_user->ID) {
  12.             $is_comment_author = ($current_user->ID == $comment->user_id);
  13.             $is_post_author = ($current_user->ID == get_post_field('post_author', $comment->comment_post_ID));
  14.         }

  15.         if ($is_admin || $is_comment_author || $is_post_author) {
  16.             return '<div class="hidden-box show" id="hidden-box-comment"><div class="hidden-text">私密评论内容</div>' . $comment_content . '</div>';
  17.         }

  18.         return $html;
  19.     }
  20.    
  21.     return $comment_content;
  22. }
  23. add_filter('get_comment_text', 'zib_private_message_hook', 10, 2);

  24. function zib_comments_action_add_private($lists, $comment) {
  25.     $current_user = wp_get_current_user();
  26.     $user_id = get_current_user_id();

  27.     if (is_user_logged_in() && (is_super_admin($user_id) || $current_user->ID == $comment->user_id)) {

  28.         $comment_ID = $comment->comment_ID;
  29.         $is_private = get_comment_meta($comment_ID, '_private', true);
  30.         $private_text = empty($is_private) ? '设为私密' : '取消私密';
  31.         $action = empty($is_private) ? 'set_private' : 'del_private';
  32.         $private_but = '<a class="comment-private-link wp-ajax-submit" form-action="' . esc_attr($action) . '" form-data="' . esc_attr(json_encode(['id' => $comment_ID])) . '" href="javascript:;"><i class="fa fa-user-secret mr10" aria-hidden="true"></i>' . $private_text . '</a>';

  33.         $lists = '<li>' . $private_but . '</li>' . $lists;
  34.     }

  35.     return $lists;
  36. }
  37. add_filter('comments_action_lists', 'zib_comments_action_add_private', 99, 2);
  38. function zib_private_comment_action() {
  39.     $response = ['reload' => true];

  40.     if (!isset($_POST['action']) || !$_POST['action']) {
  41.         zib_send_json_error(['msg' => '无效的操作类型'] + $response);
  42.     }

  43.     if (!isset($_POST['id']) || !$_POST['id']) {
  44.         zib_send_json_error(['msg' => '无效的评论ID'] + $response);
  45.     }
  46.     $comment_id = intval($_POST['id']);
  47.     $action = sanitize_text_field($_POST['action']);
  48.     $comment = get_comment($comment_id);
  49.     $post_author_id = get_post_field('post_author', $comment->comment_post_ID);
  50.     $current_user = wp_get_current_user();
  51.     if (!$comment_id || !$current_user || !($current_user->ID == $comment->user_id || current_user_can('manage_options'))) {
  52.         zib_send_json_error(['msg' => '权限不足或无效的评论ID'] + $response);
  53.     }

  54.     if ($action === 'set_private') {
  55.         update_comment_meta($comment_id, '_private', 'true');
  56.         zib_send_json_success(['msg' => '评论已设为私密'] + $response);
  57.     } elseif ($action === 'del_private') {
  58.         delete_comment_meta($comment_id, '_private');
  59.         zib_send_json_success(['msg' => '评论已公开'] + $response);
  60.     } else {
  61.         zib_send_json_error(['msg' => '无效的操作类型'] + $response);
  62.     }
  63. }
  64. add_action('wp_ajax_set_private', 'zib_private_comment_action');
  65. add_action('wp_ajax_del_private', 'zib_private_comment_action');
  66. add_action('comment_post', function($comment_id) {
  67.     if (isset($_POST['is-private'])) {
  68.         update_comment_meta($comment_id, '_private', 'true');
  69.     }
  70. });
复制代码
将下面的代码放到:/wp-content/themes/zibll/template/comments.php,不会放的看下图,我们进到文件可以搜“提交评论”,如下图!
tengfei_down (13).webp
  1.                                 <label class="but c-blue pw-1em" data-placement="top" data-toggle="tooltip" title="你的评论仅评论双方可见。" style="margin-bottom: 0px;">
  2.                                     <input name="is-private" type="checkbox">私密
  3.                                 </label>
复制代码

本帖被以下淘专辑推荐:

路虽远,行则将至;事虽难,做则必成。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

飞流广播+ 发布

系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
10-30 17:02
系统消息:柒沐已经连续答对10道难题,逆天学霸谁与争锋?!#每日答题#
10-09 09:07
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
09-24 09:00
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
09-11 11:40
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
09-02 09:17
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
08-27 08:56
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
08-20 15:12
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
08-03 10:22
系统消息:柒沐已经连续答对10道难题,逆天学霸谁与争锋?!#每日答题#
06-30 08:57
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
06-18 09:14
系统消息:清风网络已经连续答对10道难题,逆天学霸谁与争锋?!#每日答题#
04-11 09:40
系统消息:清风网络已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
04-10 09:31
系统消息:IXM77777已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
04-09 13:44
系统消息:清风网络已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
04-09 09:22
系统消息:柒沐已经连续答对10道难题,逆天学霸谁与争锋?!#每日答题#
04-09 08:52
系统消息:清风网络已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
04-08 09:24
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
04-07 09:02
系统消息:柒沐已经连续答对10道难题,逆天学霸谁与争锋?!#每日答题#
02-27 09:35
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
02-26 09:06
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
02-25 08:49
站内通告

提供资源交易、信息共享、靓号交流、技术变现、学习问答、兴趣娱乐等全面服务。

1.丰富功能系统,扩展社区特色玩法,打造最好的互联网聚集圈子。

2.准确信息真实交易,安全快捷又方便,让虚拟交易面对面。

3. 天上不会掉馅饼,话术骗术迷人心,切勿脱离平台线下交易,被骗与平台无关!

4. 欺诈骗钱,违规违法将视情受到警告&禁言&封号甚至检举至👮🏻‍♀️处理!

官方Q群:123129钉推群:BAYR2383 站长QQ:3220000000

投诉/建议/商务合作联系

fl@fllt.cn

严禁私下交易,被骗与本站无关。
违反交易细则,取证立查严惩。
  • 钉钉新帖推送群
  • 官方交流QQ群
  • 站长唯一微信号

👮曝光Ta|🧿小黑屋|📴手机页|飞流网 ( 渝ICP备2025054677号-1|电信增值许可 渝B2-20250789 )|网站地图

GMT+8, 2025-11-13 01:26 , Processed in 0.097847 second(s), 62 queries, MemCached On , Gzip On.

Based on XJ-TX X3.5 Licensed

飞流论坛 HanAnalytics icp Astro vhAstro-Theme

关灯 在本版发帖
扫一扫添加微信客服
QQ客服返回顶部
快速回复 返回顶部 返回列表