2024年3月19日 星期二
通知

WordPress 4.9.7免插件纯代码回复可见功能

回复可见功能可以防那些伸手党,拿了资源就走的,也可以给网站增加访客的粘性,同时也可以防一些采集的人,辛苦写的内容被别人复制走了,更可气的是复制你内容的网站被搜索引擎收录了,而你的没有。

有木有被说中~~~

如何正确的使用回复可见功能,下面开始直入正题

打开你主题找到functions.php文件,在?>前面添加以下内容:

//部分内容回复可见
function reply_to_read($atts, $content=null) {
extract(shortcode_atts(array("notice" => '<span class="reply_to_read">温馨提示: 此处内容需要<a href="#comments" title="评论本文">评论本文</a>审核通过后才能查看,请认真对待评论,否则不通过。</span>'), $atts));
$email = null;
$user_ID = (int) wp_get_current_user()->ID;
if ($user_ID > 0) {
$email = get_userdata($user_ID)->user_email;
//对博主直接显示内容
$admin_email = "admin@liry.cn"; //博主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('hide', 'reply_to_read');

当然还需要配一下CSS样式,保证美观

.reply_to_read {
display:inline-block;
text-align:center;
border:1px dashed #ff6666;
padding:5px;
margin:5px auto;
color:#FF6666;
width:100%;
}
.reply_to_read a {
color:#04a1ef
}
.reply_to_read a:hover {
color:#ffb300
}

如果和你主题不协调,可以自行修改一下。

为了防止有人乱填乱写评论,以及打广告,我们还需要做一些设置,

设置 — 讨论 — 在评论显示之前:勾选 和评论者先前须有评论通过了审核;

设置 — 讨论 — 评论审核:设置内容超链接数量以及其他需要审核的关键词;

设置 — 讨论 — 评论黑名单:在这设置一些敏感关键词,触发直接进垃圾评论;

说了半天忘记告诉大家怎么使用了:

【hide】隐藏内容【/hide】或者【hide notice=自定义信息】隐藏内容【/hide】

记得将【】替换成[]

感谢@枭灵酱这位网友反馈,如果是简码套简码,隐藏内容的可能显示为简码内容,而不是原本简码内容样式。

简码套简码则需要把11行的 return $content; 修改成 return do_shortcode($content);

升级版

// 评论可见
function reply_read($atts, $content=null) {
extract(shortcode_atts(array("notice" => '
<div class="down-detail"><h5>隐藏资源</h5>
<p class="down-hidden">隐藏内容:******,回复后可见!</p>
<p class="down-tip">隐藏内容需评论本文审核通过后才能查看,请认真对待,否则不通过。</p>
</div>'), $atts));
$email = null;
$user_ID = (int) wp_get_current_user()->ID;
if ($user_ID > 0) {
$email = get_userdata($user_ID)->user_email;
if ( current_user_can('level_10') ) {
return '<div class="down-detail"><h5>隐藏资源</h5><p class="down-hidden">'.do_shortcode( $content ).'</div>';
}
if ($email == $admin_email) {
return do_shortcode($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('<div class="down-detail"><h5>隐藏资源</h5><p class="down-hidden">'.do_shortcode( $content ).'</div>');
} else {
return $notice;
}
}
add_shortcode('hide', 'reply_read');

CSS样式

.down-detail {
width: auto;
border-right: 1px solid #f5f6f9;
border-left: 1px solid #f5f6f9;
}
.down-detail h5 {
padding: 10px 20px;
background: #f5f6f9;
border-left: 3px solid #81bd00;
padding-left: 5px;
color: #666;
font-weight: normal;
line-height: 15px;
font-size: 15px;
}
.down-detail p {
margin: 0 !important;
padding: 6px 10px;
line-height: 20px;
border-bottom: 1px solid #f8f8f8;
text-indent: 0 !important;
}
.down-tip .down-hidden {
font-size: 15px;
}

样式根据自己来修改

内容查看本文隐藏内容仅限注册用户查看,请先
本内容仅限有权限用户查看,如有问题可留言
疑问咨询QQ:819229574
原创文章,转载请注明: 转载自荣耀博客
本文链接地址: WordPress 4.9.7免插件纯代码回复可见功能

关于 荣耀博客

好记性,不如烂笔头。

随机推荐

2019060104162147 310x165.jpg

WordPress验证码插件 Captcha Plus v5.0.5

Captcha Plus 插件 …

发表回复