专注云计算教学
新手上云第一站

wordpress文章添加回复可见功能

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

<pre class="brush:php;toolbar:false;">//评论可见
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 = "aprioy@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');

代码中的aprioy@qq.com请改为你的管理员邮箱,直接对该邮箱的用户展示内容。 将以上的代码放到主题目录下的

functions.php

即可使用。 调用方法 在可视化或者编辑框下输入短代码 [#reply]隐藏的内容[#/reply] ,把#号去掉。中间可以改为你要隐藏的内容。

赞(6)
未经允许不得转载:WM云建站 » wordpress文章添加回复可见功能

评论 5

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #1

    感谢分享。

    Aprioy5年前 (2019-07-29)回复
  2. #2

    干嘛的

    爱豆豆的烦恼生活此处略去三个字哈哈哈5年前 (2019-07-30)回复