代码方式实现WordPress文章内容回复可见

如何你想了解如何给wordpress博客添加“隐藏内容回复可见”功能,请继续阅读本文!^_^首先推荐一款实现该功能的插件:easy2hide 使用方法,启用插件后,在编辑文章时加入:

<!--easy2hide start{reply_to_this=true}-->一些隐藏内容<!--easy2hide end-->

start{reply_to_this=true} 是设置“只有回复本贴的人才能查看隐藏内容”,也可将引用简写为:

<!--easy2hide start{}-->一些隐藏内容<!--easy2hide end-->

效果为:凡是之前在你博客上回过贴的人都能看到此贴隐藏内容 请自行解读这两句中文的区别所在…

但开启easy2hide插件会导致后台的ckeditor-for-wordpress插件无法正常加载使用!小汗一个….若没使用ckeditor-for-wordpress插件的同学,可直接使用easy2hide插件即可正常使用“隐藏内容回复可见”功能啦! 下面来说说不用插件的实现方法,把下面代码加到主题functions.php文件中:

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;
//对博主直接显示内容 豫章小站blog.mydns.vip
$admin_email = "test@test.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;
}
//豫章小站blog.mydns.vip
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');

编辑文章时调用格式如下:

[ reply]评论后可见的内容[ /reply] 或者 [ reply notice="豫章小站blog.mydns.vip提醒"]评论后可见的内容[ /reply]

注意实际使用时,去掉reply标签里面的空格。

原文地址:https://blog.mydns.vip/1156.html

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-62778877-8261;邮箱:jenny@west.cn。本站原创内容未经允许不得转载,或转载时需注明出处::西部数码资讯门户 » 代码方式实现WordPress文章内容回复可见

赞 (1)