WordPress 添加回覆可見小工具

6,320 1

有時候我們寫了一篇有價值的文章,為保護辛勤勞動或者不給伸手黨一絲機會,再或者是為了增加評論量,所以就想用到評論可見的功能,我參考了一下知更鳥的回覆可見,然後把他的代碼獨立出來,下面會分享給小夥伴們喔

代碼

PHP
// 评论可见
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],中間可以改為你要隱藏的內容。

評論

(1)
  1. Chrome 78 Windows 10

    没用啊,还是现实隐藏内容

發佈評論

© 2026 一紙忘憂. 保留所有權利.

一紙忘憂的個人網站,關於技術探索與日常生活,記錄 Web 開發、後端與 DevOps 實踐,分享軟體工具、數位體驗,以及程式碼之外的見聞與思考。