WordPress文章用代码实现添加回复可见功能

2018年11月9日13:34:31 发表评论 1,569 views

分享一个WordPress网站回复可见内容的代码,网上有很多这样的教程,但是很多都已经用不了了,插件装多了网站还卡。

WordPress文章用代码实现添加回复可见功能

 

将以下的代码添加到你主题目录的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. if ($params['reply_to_this'] == 'true') {
  11. global $current_user;
  12. get_currentuserinfo();
  13. if ($current_user->ID) {
  14. $email = $current_user->user_email;
  15. else if (isset($_COOKIE['comment_author_email_'.COOKIEHASH])) {
  16. $email = $_COOKIE['comment_author_email_'.COOKIEHASH];
  17. }
  18. $ereg = "^[_\.a-z0-9]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,5}$";
  19. if (eregi($ereg$email)) {
  20. global $wpdb;
  21. global $id;
  22. $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_author_email = '".$email."' and comment_post_id='".$id."'and comment_approved = '1'");
  23. if ($comments) {
  24. $stats = 'show';
  25. }
  26. }
  27. $tip = __('<span class="vihide">抱歉,隐藏内容 <a href="#comments">回复</a> 后刷新可见</span>', 'hide');
  28. else {
  29. if (isset($_COOKIE['comment_author_'.COOKIEHASH]) or current_user_can('level_0')) {
  30. $stats = 'show';
  31. }
  32. $tip = __();
  33. }
  34. $hide_notice = $tip;
  35. if ($stats == 'show') {
  36. $content = str_replace($matches[0], $matches[2], $content);
  37. else {
  38. $content = str_replace($matches[0], $hide_notice$content);
  39. }
  40. }
  41. return $content;
  42. }
  43. add_action('admin_footer', 'hide_footer_admin');

 

CSS样式

  1. .vihide{display:inline-block;text-align:center;border: 2px dashed #ff6666;padding:8px;margin:10px auto;color:#FF6666;width:100%;}
  2. .vihide a{color:#04a1ef}
  3. .vihide a:hover{color:#ffb300}

 

代码调用

后台编辑文章的时候添加以下代码

站长小智

发表评论

您必须登录才能发表评论!