xiaobai 发表于 2020-6-24 10:57:59

WordPress文章内容免插件实现回复可见

这个功能类似于Discuz论坛的“回复可见”功能,增加网友间互动的一种方式,以下举两个例子。
实现方式一
将以下代码添加到主题的functions.php文件最后一个?>的前面。

//文章部分内容回复可见
function reply_to_read($atts, $content=null) {
extract(shortcode_atts(array("notice" => '
<span style="color: red;">温馨提示:</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 = "xxxxx@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');

PS:将上面代码中的xxxxx@qq.com替换为站点管理员的邮箱地址,以实现管理员直接可见。
使用方法:{reply}测试隐藏内容是否成功{/reply}
{}改成[],所有符号英文字符

实现方式二
核心代码,添加到主题functions.php中

function hide($atts,$content=null,$code=""){
extract(shortcode_atts(array("reply_to_this"=>'true'),$atts));
global $current_user;
get_currentuserinfo();
if($current_user->ID) $email = $current_user->user_email;
if($reply_to_this=='true'){
if($email){
global $wpdb;
global $id;
$comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_author_email = '".$email."' and comment_post_id='".$id."'and comment_approved = '1'");
}
if(!$comments) $content = '<div class="hide_notice">抱歉,只有<a href="'.wp_login_url(get_permalink()).'" rel="nofollow">登录</a>并在本文发表评论才能阅读隐藏内容</div>';
}else{
if($email){
global $wpdb;
global $id;
$comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_author_email = '".$email."' and comment_approved = '1'");
}
if(!$comments) $content = '<div class="hide_notice">抱歉,只有<a href="'.wp_login_url(get_permalink()).'" rel="nofollow">登录</a>并在本站任一文章发表评论才能阅读隐藏内容</div>';
}
if($comments) $content = '<div class="unhide"><div class="info">以下为隐藏内容:</div>'.$content.'</div>';
return $content;
}
add_shortcode('hide','hide');

CSS样式
里面有个图片链接需要自己去扒一图片加上去

.hide_notice{overflow:hidden;padding:8px 8px 8px 24px;border:1px dashed #ff9a9a;background:url(这里是小锁的图片链接,自己扒一下吧...) no-repeat 6px 50%;font-size:12px;color:#ff0000;margin-bottom:15px}
.hide_notice a{color:#ff4b4b}
.unhide{padding:8px;border:1px dashed #ff9a9a;margin-bottom:15px}
.unhide .info{font-size:12px;color:#ff0000}

使用方法:
当前文章回复可见:{hide reply_to_this=”true”}测试隐藏内容是否成功{/hide}
任一文章回复可见:{hide reply_to_this=”false”}测试隐藏内容是否成功2{/hide}
{}改成[],所有符号英文字符
页: [1]
查看完整版本: WordPress文章内容免插件实现回复可见