纯代码实现wordpress文章隐藏内容评论可见
在很多网站上都看过这个效果,比如说知己知彼网站,他的部分资源是需要我们评论后才能下载的,那么这个到底有什么用呢,对我而言,除了拿来装逼,还可以增加我的评论数量,不多说,先看看效果:

其实WordPress有很多的插件可以实现这个功能,比如说Easy2Hide,但是插件当然是越少越好,下面我就来说说怎么用代码实现这个功能:
网上的原文:
在当前主题的functions.php文件添加以下代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
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 = "xxx@boke8.net"; //把左面的邮箱换成博主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'); |
注:把代码中的“xxx@boke8.net”换成博主邮箱地址

我修改后的代码:
我开始直接用的网上的代码,但是发现显示出来的效果有错误,于是自己修改了一下标签和样式,然后修改了自己的邮箱号码。其他部分的代码并不需要修改。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
function reply_to_read($atts, $content=null) { extract(shortcode_atts(array("notice" => '<p class="reply-to-read" style="text-align:center; border:2px solid #f00; border-style:dotted; border-radius:4px; padding:5px; margin:10px;"><strong style="color: red;">温馨提示:</strong>为了避免资源链接被和谐,此处内容需要您<strong><a href="#respond" title="点击进行评论"> 回复评论 </a></strong>后才能查看, 评论后请 <strong><a href="javascript:location.reload()" title="点击刷新"> 刷新!</a></strong></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 = "970852638@qq.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'); |
这样就可以让别人只有回复了评论才能下载你网站资源的效果,是不是很有逼格,我今天刚使用了这个就有了新的评论呢。
参考网址:http://www.boke8.net/the-wp-post-visible-when-comments.html

浙公网安备 33010602011771号