返回列表 发布新帖
查看: 47|回复: 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-6 20:13:56 | 查看全部 |阅读模式 浙江金华

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

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

×
这是一款子比主题私密评论的功能,这个功能带有设置评论的私密和删除私密的功能,非常实用的一个功能,我觉得很不错的一个加密评论,喜欢的自行部署吧!
tengfei_down (7).webp tengfei_down (6).webp

代码部署
首先我们要在目录:/wp-content/themes/zibll/func.php文件,没有这个文件自己创一个,记住创建之后要记得加php头,话不多说直接开始,将下面的代码放到:/wp-content/themes/zibll/func.php文件里面
tengfei_down (8).webp
  1. function zyx_scripts(){
  2.   if (!is_admin()) {
  3.         $script = array(
  4.             'smminjs' => '改为你的js链接',
  5.         );
  6.         foreach( $script as $k => $v ){
  7.             wp_register_script( $k, $v, array(), '2.4.0', true);
  8.         };

  9.         wp_enqueue_script('smminjs');
  10.         
  11.         if (is_singular()) {
  12.             wp_enqueue_script('smminjs');
  13.         };

  14.     }
  15. }
  16. add_action('wp_enqueue_scripts', 'zyx_scripts');

  17. function zyx_private_message_hook( $comment_content , $comment){
  18.     $comment_ID = $comment->comment_ID;
  19.     $parent_ID = $comment->comment_parent ? $comment->comment_parent : '';
  20.     $parent_email = get_comment_author_email($parent_ID);
  21.     $is_private = get_comment_meta($comment_ID,'_private',true);
  22.     $email = $comment->comment_author_email;
  23.     $current_commenter = wp_get_current_commenter();
  24.   $current_user = wp_get_current_user();
  25.     $html = '<span style="color:#558E53"><i class="fa fa-lock fa-fw"></i>该评论为私密评论</span>';
  26.   if ( $is_private ) {
  27.       if ( !is_user_logged_in() && $current_commenter['comment_author_email'] == '' ) {
  28.         return $comment_content = $html;
  29.       }else
  30.     if ($current_commenter['comment_author_email'] == '' && $current_user->user_email == $parent_email || current_user_can('delete_user') || $current_user->user_email == $email || $current_commenter['comment_author_email'] == $email || $parent_email == $current_commenter['comment_author_email'] && $current_commenter['comment_author_email'] !== ''){
  31.       return $comment_content = '#私密# ' . $comment_content;
  32.     }
  33.     return $comment_content = $html;
  34.   }
  35.     return $comment_content;
  36. }
  37. add_filter('get_comment_text','zyx_private_message_hook',10,2);

  38. function zyx_mark_private_message( $comment_id ){
  39.     if ( $_POST['is-private'] ) {
  40.         update_comment_meta($comment_id,'_private','true');
  41.     }
  42. }
  43. add_action('comment_post', 'zyx_mark_private_message');
  44. //将某条评论设为私密评论
  45. add_action('wp_ajax_nopriv_mrhe_private', 'zyx_private');
  46. add_action('wp_ajax_mrhe_private', 'zyx_private');
  47. function zyx_private(){
  48.   $comment_id = $_POST["p_id"];
  49.    $action = $_POST["p_action"];
  50.   if ( $action == 'set_private'){
  51.      update_comment_meta($comment_id, '_private', 'true');
  52.     }
  53.     if ($action == 'del_private'){
  54.         delete_comment_meta($comment_id, '_private','true');
  55.     }
  56.     echo 'ok';
  57.    die;
  58. }
  59. //挂载到评论底部
  60. function zyx_footer_info_add_private($info, $comment) {
  61.     if ( current_user_can( 'manage_options' ) ) {
  62.         $comment_ID = $comment->comment_ID;
  63.         $i_private = get_comment_meta($comment_ID, '_private', true);
  64.         $flag = ''; // 初始化 $flag 为空字符串
  65.         if (empty($i_private)) {
  66.             $flag .= ' - <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  data-actionp="set_private" data-idp="' . get_comment_id() . '" id="sp" class="sm">(<span class="has_set_private">设为私密</span>)</a>';
  67.             $info = $info . $flag;
  68.         } else {
  69.             $flag .= ' - <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  data-actionp="del_private" data-idp="' . get_comment_id() . '" id="sp" class="sm">(<span class="has_set_private">删除私密</span>)</a>';
  70.             $info = $info . $flag;
  71.         }
  72.     }
  73.     return $info;
  74. }
  75. add_filter('comment_footer_info', 'zyx_footer_info_add_private', 99, 2);
复制代码
然后我们将下面的代码放到:/wp-content/themes/zibll/template/comments.php,不会放的看下图
  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">私密</label>
复制代码
可以看func文件那个地方,那个里面的代码是不是有一个'smminjs' => '改为你的js链接',那么这个就是js代码的链接,怎么弄链接就不用说了吧,自己创建文件放进去然后拿到目录链接放里面!
  1. $(function() {
  2.   $(".links-card ul.list-inline img").each(function() {
  3.     if ("" == $(this).attr("data-src")) {
  4.       var a = "" + $(this).parent().attr("href");
  5.       $(this).attr("src", a)
  6.     }
  7.   }), $(document).on("click", ".sm", function() {
  8.     var a = $(this);
  9.     if (a.hasClass("private_now")) return notyf("您之前已设过私密评论", "warning"), !1;
  10.     a.addClass("private_now");
  11.     var t = a.data("idp"),
  12.       e = a.data("actionp"),
  13.       n = a.children(".has_set_private"),
  14.       r = {
  15.         action: "mrhe_private",
  16.         p_id: t,
  17.         p_action: e
  18.       };
  19.     return $.post("/wp-admin/admin-ajax.php", r, function(a) {
  20.       n.html(a)
  21.     }), !1
  22.   })
  23. });
复制代码

本帖被以下淘专辑推荐:

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

本版积分规则

飞流广播+ 发布

系统消息:柒沐已经连续答对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:40 , Processed in 0.084025 second(s), 61 queries, MemCached On , Gzip On.

Based on XJ-TX X3.5 Licensed

飞流论坛 HanAnalytics icp Astro vhAstro-Theme

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