WordPress内容指定用户可见

发布于 / 教程 / 0 条评论

首先申明原始代码博主也是网上找的,就是评论回复可见的代码,放到主题function.php里。原始内容如下:

function reply_to_read($atts, $content=null) {
        extract(shortcode_atts(array("notice" => '<p class="reply-to-read">温馨提示: 此处内容需要<a href="#respond" rel="external nofollow"  rel="external nofollow"  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 = "xxx@aaa.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');

实现原理就是判断当前用户登录状态和用户的邮箱账号。

 $admin_email = "xxx@aaa.com"; //博主Email
            if ($email == $admin_email) {
                return $content;
            }

以上部分是对管理员的账号的用户内容可见,判断当前用户email是否为管理员的邮箱。所以要实现对指定用户可见就可以增加一个变量比如:user1_email,再和当前用户email对比。

$user1_email = "xxx@aaa.com"; //你需要设置可见的用户Email
            if ($email == $user1_email) {
                return $content;
            }

这样就实现了内容对多人可见。实际上就是增加了个判断,实现指定用户可见,其他人需要回复,完整代码就这样:

function reply_to_read($atts, $content=null) {
        extract(shortcode_atts(array("notice" => '<p class="reply-to-read">温馨提示: 此处内容需要<a href="#respond" rel="external nofollow"  rel="external nofollow"  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 = "xxx@aaa.com"; //博主Email
            if ($email == $admin_email) {
                return $content;
            }
            $user1_email = "xxx@aaa.com"; //你需要设置可见的用户Email
            if ($email == $user1_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');

博主是PHP小白,代码还可以进一步简化的。

转载原创文章请注明,转载自: LYLARES'S BLOG » WordPress内容指定用户可见

Not Comment Found