WordPress小功能很多,但是真正能用到的功能也就那几个,回复可见功能不仅可以增加网站的人气,还能给人一种逼格有没有。
注意一下:请勿开启评论可以匿名,就是不需要填写任何信息就能提交评论的,要不然会无法显示。
废话不多说,下面开始教程,如何让WordPress代码实现回复可见小功能。
在functions.php
中添加代码:
- add_filter('the_content', 'hide');
- add_filter('comment_text','hide');
- function hide($content) {
- if (preg_match_all('/<!--hide start{?([\s\S]*?)}?-->([\s\S]*?)<!--hide end-->/i', $content, $matches)) {
- $params = $matches[1][0];
- $defaults = array('reply_to_this' => 'false');
- $params = wp_parse_args($params, $defaults);
- $stats = 'hide';
- if ($params['reply_to_this'] == 'true') {
- global $current_user;
- get_currentuserinfo();
- if ($current_user->ID) {
- $email = $current_user->user_email;
- } else if (isset($_COOKIE['comment_author_email_'.COOKIEHASH])) {
- $email = $_COOKIE['comment_author_email_'.COOKIEHASH];
- }
- $ereg = "^[_\.a-z0-9]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,5}$";
- if (eregi($ereg, $email)) {
- global $wpdb;
- global $id;
- $comments = $wpdb->get_results("SELECT * FROM$wpdb->comments WHERE comment_author_email = '".$email."' and comment_post_id='".$id."'and comment_approved = '1'");
- if ($comments) {
- $stats = 'show';
- }
- }
- $tip = __('<span class="vihide">抱歉,隐藏内容 <a href="#comments">回复</a> 后刷新可见</span>', 'hide');
- } else {
- if (isset($_COOKIE['comment_author_'.COOKIEHASH]) or current_user_can('level_0')) {
- $stats = 'show';
- }
- $tip = __();
- }
- $hide_notice = $tip;
- if ($stats == 'show') {
- $content = str_replace($matches[0], $matches[2], $content);
- } else {
- $content = str_replace($matches[0], $hide_notice, $content);
- }
- }
- return $content;
- }
- add_action('admin_footer', 'hide_footer_admin');
加上之后我们在美化一下CSS:
- .vihide {
- display:inline-block;
- text-align:center;
- border: 2px dashed #ff6666;
- padding:8px;
- margin:10px auto;
- color:#FF6666;
- width:100%;
- }
- .vihide a {
- color:#04a1ef
- }
- .vihide a:hover {
- color:#ffb300
- }
那么如何调用呢?
很简单,只需要在写文章的时候转到文本模式,填写:
- <!--hide start{reply_to_this=true}-->隐藏内容<!--hide end-->
参与评论
- 自力更生游客 我想问一下这个是针对一个帖子可见还是说回复过一个帖子所有帖子都能看的到还有一个问题就是这个回复可见跟crayon syntax highlighter插件能同时用吗 可以同时用的话具体要怎么写呢4年前 (2019-03-05)1楼