WordPress 回复可见【非插件】

网上随便搜了一下,发现这个功能的文章基本都是抄来抄去抄的。我也抄了一份发现不大好用,因为发表评论之后看了一下发现根本没有cookie信息。如果要使用这个功能首选需要开启下面的选项:

下面就是详细步骤了:

1.改完上面的设置之后按照下面的方法修改主题的functions.php添加以下代码:

function reply_to_read($atts, $content=null) {
    extract(shortcode_atts(array("notice" => '<p class="reply-to-read">温馨提示: 此处隐藏内容需要<a href="#respond" title="发表评论">发表评论</a>,并且审核通过后才能查看。<br />(发表评论请勾选 <strong>在此浏览器中保存我的显示名称、邮箱地址和网站地址,以便下次评论时使用。</strong>)</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 = array("root@obaby.org.cn","obaby.lh@gmail.com","obaby.lh@163.com");
        if(in_array($email,$admin_email)) {
            // if ($email == $admin_email){
            return $content;
        }
    } else if (isset($_COOKIE['comment_author_email_'.COOKIEHASH])) {
        $email = str_replace('%40', '@', $_COOKIE['comment_author_email_'.COOKIEHASH]);
        //return $email;
    } 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');

 

2.在需要隐藏内容的地方添加以下代码即可:

最终展示效果就是第一张图的效果。

☆版权☆

* 网站名称:obaby@mars
* 网址:https://h4ck.org.cn/
* 个性:https://oba.by/
* 本文标题: 《WordPress 回复可见【非插件】》
* 本文链接:https://h4ck.org.cn/2021/05/8116
* 短链接:https://oba.by/?p=8116
* 转载文章请标明文章来源,原文标题以及原文链接。请遵从 《署名-非商业性使用-相同方式共享 2.5 中国大陆 (CC BY-NC-SA 2.5 CN) 》许可协议。


猜你喜欢:

2 comments

  1. Level 1
    Microsoft Edge 93 Microsoft Edge 93 Windows 10 Windows 10 cn北京市 联通

    看看里面隐藏的,自己站也搞一个 感谢分享!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注