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

代码

// 评论可见
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 = "xinyewl@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');

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

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

调用方法

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

如果觉得我的文章对你有用,请随意赞赏