今天看到斯蒂芬周的博客中有“本文隐藏内容登录后才可以查看”,于是想也有这样的功能,但是只要变成回复就可以查看,百度了很多内容,发现都不太行,插件好像也不大行,岁月小筑的代码发生经过测试好像是可以的。

1)在functions.php文件中插入如下代码:
/*回复可见Functions*/
function reply_to_read($atts, $content=null) {
extract(shortcode_atts(array("notice" => '<p class="reply-to-read">温馨提示: 此处内容需要<a href="#respond" title="评论本文">评论本文</a>后才能查看.</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 = "public@xjh.me"; //博主自己的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');
2)插入短代码

[reply]欢迎大家来的我的网站,以后会分享一些有用的视频和文章[/reply]
ps:发现一定得先注册才行呀!不然回复无效。
评论列表(3条)
我想看是否是成功了
再试一次
可以评论吗