wordpress代码实现内容回复可见功能

今天给我的博客加了文章内容回复可见的功能,代码版的,网上有些都已经失效了,我特意重新整理了一份出来,并且在后台编辑器也加了快捷按钮,下面言归正传,总共分为 3 步。

1. 在 functions.php 中加入下列代码:

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

2.在 functions.php 加入下面代码,实现编辑器后面快捷按钮功能。

  1. // 添加编辑器按钮 - 回复可见
  2. function reply_view_tags($mce_settings) {
  3. ?>
  4. <script type="text/javascript">
  5. QTags.addButton( 'qiuzhuti_reply_view', '回复可见', '<!--hide start{reply_to_this=true}-->', '<!--hide end-->' );
  6. </script>
  7. <?php
  8. }
  9. add_action('after_wp_tiny_mce', 'reply_view_tags');

3. 加入 css 样式,美化一下,可自行调整。

  1. /*回复可见*/
  2. .vihide{display:inline-block;text-align:center;border: 2px dashed #ff6666;padding:8px;margin:10px auto;color:#FF6666;width:100%;}
  3. .vihide a{color:#04a1ef}
  4. .vihide a:hover{color:#4ec6c8}
© 版权声明
THE END
喜欢就支持以下吧
点赞0赞赏
分享
相关推荐
  • 暂无相关文章
评论 抢沙发