WordPress小功能很多,但是真正能用到的功能也就那几个,回复可见功能不仅可以增加网站的人气,还能给人一种逼格有没有。

注意一下:请勿开启评论可以匿名,就是不需要填写任何信息就能提交评论的,要不然会无法显示。

废话不多说,下面开始教程,如何让WordPress代码实现回复可见小功能

functions.php中添加代码

  1. add_filter('the_content', 'hide');  
  2. add_filter('comment_text','hide');  
  3. function hide($content) {  
  4.     if (preg_match_all('/<!--hide start{?([\s\S]*?)}?-->([\s\S]*?)<!--hide end-->/i', $content$matches)) {  
  5.         $params = $matches[1][0];  
  6.         $defaults = array('reply_to_this' => 'false');  
  7.         $params = wp_parse_args($params$defaults);  
  8.         $stats = 'hide';  
  9.         if ($params['reply_to_this'] == 'true') {  
  10.             global $current_user;  
  11.             get_currentuserinfo();  
  12.             if ($current_user->ID) {  
  13.                 $email = $current_user->user_email;  
  14.             } else if (isset($_COOKIE['comment_author_email_'.COOKIEHASH])) {  
  15.                 $email = $_COOKIE['comment_author_email_'.COOKIEHASH];  
  16.             }  
  17.             $ereg = "^[_\.a-z0-9]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,5}$";  
  18.             if (eregi($ereg$email)) {  
  19.                 global $wpdb;  
  20.                 global $id;  
  21.                 $comments = $wpdb->get_results("SELECT * FROM$wpdb->comments WHERE comment_author_email = '".$email."' and comment_post_id='".$id."'and comment_approved = '1'");  
  22.                 if ($comments) {  
  23.                     $stats = 'show';  
  24.                 }  
  25.             }  
  26.             $tip = __('<span class="vihide">抱歉,隐藏内容 <a href="#comments">回复</a> 后刷新可见</span>', 'hide');  
  27.         } else {  
  28.             if (isset($_COOKIE['comment_author_'.COOKIEHASH]) or current_user_can('level_0')) {  
  29.                 $stats = 'show';  
  30.             }  
  31.             $tip = __();  
  32.         }  
  33.         $hide_notice = $tip;  
  34.         if ($stats == 'show') {  
  35.             $content = str_replace($matches[0], $matches[2], $content);  
  36.         } else {  
  37.             $content = str_replace($matches[0], $hide_notice$content);  
  38.         }  
  39.     }  
  40.     return $content;  
  41. }  
  42. add_action('admin_footer', 'hide_footer_admin');  

加上之后我们在美化一下CSS:

  1. .vihide {  
  2.     display:inline-block;  
  3.     text-align:center;  
  4.     border2px dashed #ff6666;  
  5.     padding:8px;  
  6.     margin:10px auto;  
  7.     color:#FF6666;  
  8.     width:100%;  
  9. }  
  10. .vihide a {  
  11.     color:#04a1ef  
  12. }  
  13. .vihide a:hover {  
  14.     color:#ffb300  
  15. }  

那么如何调用呢?

很简单,只需要在写文章的时候转到文本模式,填写:

  1. <!--hide start{reply_to_this=true}-->隐藏内容<!--hide end-->  

参与评论

  • 自力更生游客 我想问一下这个是针对一个帖子可见还是说回复过一个帖子所有帖子都能看的到还有一个问题就是这个回复可见跟crayon syntax highlighter插件能同时用吗 可以同时用的话具体要怎么写呢
    4年前 (2019-03-05)
    1楼
    回复