WordPress添加文章回复可见代码

有时候我们写了一篇有价值的文章,为保护辛勤劳动或者不给伸手党一丝机会,再或者是为了增加评论量,所以就想用到评论可见的功能,我参考了一下知更鸟的回复可见,然后把他的代码独立出来,下面会分享给小伙伴们喔

代码

//评论可见
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 = "2935868921@qq.com";
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');

代码中的2935868921@qq.com请改为你的管理员邮箱,直接对该邮箱的用户展示内容。

将以上的代码放到主题目录下的 functions.php 即可使用。

调用方法

在可视化或者编辑框下输入短代码,中间可以改为你要隐藏的内容。

 [reply]隐藏的内容[/reply] 
声明:本站部分文章来源于网络,如有危害到您的合法权益请联系客服

给TA打赏
共{{data.count}}人
人已打赏
WP技术WP插件

WordPress制作微信小程序,源码+使用教程

2022-11-17 22:35:37

7b2WP技术

WordPress代码为主题添加申友情链接功能

2022-11-20 23:44:48

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索