想必大家在下载一些软件或者插件的时候都会发现,很多网站为了防止大量的无效灌水评论,通常在资源下载的位置设置一个“评论后可见”的功能,必须要注册账号并进行评论才能看到下载链接。
这种做法表面上看似是为了防止哪些无效的灌水回复,其实在防止灌水回复的同时还未网站新增了一个有需求的用户,并且还增加了网站评论,可谓是一者多得啊。
很多博友一般会借助插件实现“隐藏内容评论后可见”效果,比如Easy2Hide插件,但是博主不推荐这样做,因为插件会影响站点的加载速度,故而博主今天就是要教大家怎么使用代码实现这种功能。
打开wordpress主题下的functions.php文件
将以下代码复制粘贴到functions.php文件中,并且把代码中的邮箱更换成自己的
function reply_to_read($atts, $content=null) { extract(shortcode_atts(array("notice" => '<p class="reply-to-read" style="border-width: 1px 1px 1px 1px;border-color: #F2F2F2;line-height: 150%;"><blockquote><font color="#ff0000"><b>温馨提示</b></font>: 隐藏内容需要<a href="#respond" title="点击进行评论"> 回复评论 </a>后才能查看, 评论后请 <strong><a href="javascript:location.reload()" title="点击刷新"> 刷新 !</a></strong>.</blockquote></p>'), $atts)); $email = null; $user_ID = (int) wp_get_current_user()->ID; if ($user_ID > 0) { $email = get_userdata($user_ID)->user_email; //对博主直接显示内容 $admin_email = "www@jayseoer.com"; //把左面的邮箱换成博主Email if ($email == $admin_email) { return $content; } } else if (isset($_COOKIE['comment_author_email_' . COOKIEHASH])) { $email = str_replace('%40', '@', $_COOKIE['comment_author_email_' . COOKIEHASH]); } else { return $notice; } if (empty($email)) { return $notice; } global $wpdb; $post_id = get_the_ID(); $query = "SELECT `comment_ID` FROM {$wpdb->comments} WHERE `comment_post_ID`={$post_id} and `comment_approved`='1' and `comment_author_email`='{$email}' LIMIT 1"; if ($wpdb->get_results($query)) { return do_shortcode($content); } else { return $notice; } } add_shortcode('reply', 'reply_to_read');
撰写文章时使用[reply][/reply]把需要实现“隐藏内容评论后可见”的内容框起来。
[reply]评论可见的内容[/reply]
下图是“隐藏内容评论后可见”的前端展示效果
如果你的站点开启了评论审核功能则需要评论通过审核后该功能才会生效。
懂得修改代码的博友可以自行修改代码的CSS样式以及隐藏文件的温馨提示语。
版权声明:若无特殊注明,本站文章皆为王金亮博客原创,转载请保留文章出处。
联系客服