wordpress通过代码实现“内容评论后可见”方法

1、将下面的代码添加到主题的 functions.php 文件:

  1. function reply_to_read($atts$content=null) {
  2.         extract(shortcode_atts(array("notice" => '<p class="reply-to-read">温馨提示: 此处内容需要<a href="#respond" title="评论本文">评论本文</a>后才能查看.</p>'), $atts));
  3.         $email = null;
  4.         $user_ID = (int) wp_get_current_user()->ID;
  5.         if ($user_ID > 0) {
  6.             $email = get_userdata($user_ID)->user_email;
  7.             //对博主直接显示内容   
  8.             $admin_email = "fujieace@126.com"//博主Email   
  9.             if ($email == $admin_email) {
  10.                 return $content;
  11.             }
  12.         } else if (isset($_COOKIE['comment_author_email_' . COOKIEHASH])) {
  13.             $email = str_replace('%40', '@', $_COOKIE['comment_author_email_' . COOKIEHASH]);
  14.         } else {
  15.             return $notice;
  16.         }
  17.         if (emptyempty($email)) {
  18.             return $notice;
  19.         }
  20.         global $wpdb;
  21.         $post_id = get_the_ID();
  22.         $query = "SELECT `comment_ID` FROM {$wpdb->comments} WHERE `comment_post_ID`={$post_id} and `comment_approved`='1' and `comment_author_email`='{$email}' LIMIT 1";
  23.         if ($wpdb->get_results($query)) {
  24.             return do_shortcode($content);
  25.         } else {
  26.             return $notice;
  27.         }
  28.     }
  29.     add_shortcode('reply', 'reply_to_read');

注:请自行修改第8行的邮件为管理员的。如果你的网站使用了ajax免刷新提交评论,你可能需要修改第2行的提示文字,提示访客评论后刷新页面来查看隐藏内容。

 

2、编辑文章时,切换到“文本”状态下:使用下面的代码:

  1. 【reply】评论可见的内容【/reply】

或者

  1. 【reply notice="自定义的提示信息"】评论可见的内容【/reply】

 

3、第2步的【】 全部 替换成 []

我说一下为什么要这样替换,其实我也想这样直接把源码直接展示出来给大家,但是如果我直接这样走,html它会直接给我解析了,这就没办法了;只好出此下策;

 

注意:此代码在以前是只要不评论都不可见隐藏内容,为了用户体验更好,后来改进了功能,添加了“对管理员不隐藏”的判断。

 

wordpess代码评论可见内容

 

不管你是用wordpress评论可见插件:easy2hide;还是用本页的代码,你一定要知道它们各自的优点和缺点,如果我们用此页的代码开启功能,那么:每一个隐藏的内容是不是需要手工加代码?如果有一天就算你删除了 functions.php里面的文件,但是你的网站内容页依旧还是有这些代码的。你总不能一个一个都去手工删除吧!

 

反之,用插件却不一样了,插件可以随时启用,也可关闭。并不影响!唯一的是:插件可能会影响一些服务器的性能,如果你是云服务器,这些都不是事;一般来说,只要wordpress插件不超过15个,我觉得都还是蛮正常的;

    A+
发布日期:2017年03月01日 22:38:01  所属分类:WordPress
最后更新时间:2017-03-01 22:43:33
付杰
  • ¥ 68.0元
  • 市场价:168.0元
  • ¥ 498.0元
  • 市场价:998.0元
  • ¥ 598.0元
  • 市场价:2980.0元
  • ¥ 99.0元
  • 市场价:129.0元

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

目前评论:1   其中:访客  0   博主  0

  1. 头像 我是鱼谁是水 4

    为什么不直接用插件?插件方便的多。