WordPress – 为文章增加评论才可查看相关隐藏内容的代码
原创文章,作者:StubbornHuang,如若转载,请注明出处:《WordPress – 为文章增加评论才可查看相关隐藏内容的代码》https://www.stubbornhuang.com/811/
1 比较普通的CSS的代码
在主题的function.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 = "xxxxxxx@xx.com"; //把左面的邮箱换成博主Email
if (email == admin_email) {
returncontent;
}
} else if (isset(_COOKIE['comment_author_email_' . COOKIEHASH])) {email = str_replace('%40', '@', _COOKIE['comment_author_email_' . COOKIEHASH]);
} else {
returnnotice;
}
if (empty(email)) {
returnnotice;
}
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 {
returnnotice;
}
}
add_shortcode('reply', 'reply_to_read');
2 增加了红点边框的CSS的代码
在主题的function.php中加入以下代码:
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 = "xxxxxxx@xx.com"; //把左面的邮箱换成博主Email
if (email == admin_email) {
returncontent;
}
} else if (isset(_COOKIE['comment_author_email_' . COOKIEHASH])) {email = str_replace('%40', '@', _COOKIE['comment_author_email_' . COOKIEHASH]);
} else {
returnnotice;
}
if (empty(email)) {
returnnotice;
}
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 {
returnnotice;
}
}
add_shortcode('reply', 'reply_to_read');
3 修改CSS样式
大家可以根据自己的需要修改上述代码extract语句中css自己定制化的css样式。
样式示例:
4 使用方法

5 注意的点
如果站点在后台设置了评论审核,那么游客评论之后需要站点管理员审核评论通过之后刷新页面才会看到隐藏内容,所以如果要使用该功能建议关闭评论审核功能。
当前分类随机文章推荐
- WordPress - 为文章增加评论才可查看相关隐藏内容的代码
- WordPress - 后台登录成功/失败发送邮件给网站管理员
- WordPress - 在浏览器的console输出信息
- WordPress - 纯代码在文章内容末尾添加当前文章同一分类下的随机推荐文章
- WordPress - 应对暴力破解登录和规避DDOS的几种方法
- WordPress - 插件WP Editor.md 在网站更换为https后无法正确加载
- WordPress - 修改管理后台登录地址,防止恶意爆破
- WordPress - $Post WP_Post对象的属性
- WordPress - PhpStudy本地环境修改固定链接打不开网页404错误
- WordPress - robots.txt
全站随机文章推荐
- WordPress - 纯代码在文章内容末尾添加当前文章同一分类下的随机推荐文章
- 资源分享 - Polygon Mesh Processing英文高清PDF下载
- 资源分享 – Fluid Simulation for Computer Graphics, Second Edition英文高清PDF下载
- WordPress - 后台登录成功/失败发送邮件给网站管理员
- 资源分享 - 白话大数据与机器学习(高扬著)PDF下载
- Centos7 - 防火墙操作以及开启和关闭特定端口
- WordPress - 站点底部显示站点运行时间
- UnrealEngine4 - 关于UObject被自动GC垃圾回收的巨坑
- 我的开源项目 - 支持C++11特性的定时器TinyTimer
- 资源分享 - Computational Geometry:An Introduction(Franco P.Preparata, and Michael Shamos)英文高清PDF下载