【WordPress】内容回复可见的实现方法
本文最后更新于 113 天前,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

WP内容回复可见

纯代码实现

在当前网站主题下的 functions.php 文件内加入以下代码:

//部分内容隐藏回复可见
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 = "[email protected]"; //自己的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');

将代码里的博主Email邮箱替换成自己的,添加好后,保存即可!

以后在发布文章的时候,只需添加以下短代码即可实现给指定内容设置回复可见了。注意:实际使用请去除 #

[#reply]
评论可见的内容
[#/reply]

或

[#reply notice="自定义的提示信息"]
评论可见的内容
[#/reply]

自定义插件实现

上述纯代码实现方法会带来一个问题,那就是当主题更新之后,修改的内容可能会被覆盖。
同样的代码,自定义成插件可解决这个问题。

WordPress 安装目录下 /wp-content/plugin 新建文件夹,取名 reply-for-view,在该文件夹中新建文件 index.php 并添加以下代码。

<?php
/*
Plugin Name: Reply for View
Description: 将部分内容隐藏,回复可见。
*/

//部分内容隐藏回复可见
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 = "[email protected]"; //自己的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 for View 的插件,启用即可。
使用方法跟纯代码实现方法一样。这样功能不会受主题更新影响,若插件丢失或禁用,文章内隐藏内容会直接显示出来。

回复可见

【END】

版权归属: E家之长
本文链接: https://www.5iehome.cc/archives/wordpress-reply-for-invisible-content.html
许可协议: 本文使用《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》协议授权
暂无评论

发送评论 编辑评论


上一篇
下一篇