> ## Content Index
> Fetch the complete content index at: https://sgpublic.xyz/llms.txt
> Use this file to discover other available public pages before exploring further.

# Wordpress 设置回复可见
- URL: https://sgpublic.xyz/p/2021/10/6aac9cb1810a4800015335ec/
- Published: 2021-10-03T02:39:00.000Z
- Updated: 2021-10-03T02:39:00.000Z
- Author: Haven Madray
- Tags: 基础设施, #wp-post

Wordpress 回复可见实现现在网上都是清一色的修改当前主题的 `function.php` 文件，这样会带来一个问题，那就是当主题更新之后，修改的内容可能会被覆盖。

这里给出一个解决方案，同样的代码，添加为插件可以完美解决这个问题。

在 Wordpress 安装目录下 `/wp-content/plugin` 新建文件夹，名称自取，然后新建文件 `index.php` 并添加以下内容

```
<?php
/*
Plugin Name: Reply to View
Description: 将部分内容隐藏，回复可见。
Version: 1.0
*/

//部分内容评论可见
function reply_to_read($atts, $content = null, $admin_email = []) {
    extract(shortcode_atts(array("notice" => '<p class="reply-to-read">此处内容需要 <a href="#comments" 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');

```

保存后在 Wordpress 插件列表中会出现名为 `Reply to View` 的插件，启用即可。

使用时将需要回复可见的内容直接添加到正文，并用 `[reply][/reply]` 包裹起来即可。例如：

```
[reply]
隐藏内容
[/reply]
```

添加为插件之后，功能不会受主题更新影响，若插件不慎丢失，文章内隐藏内容会直接显示出来。