wordpress代码:在文章中设置回复可见内容

今天看到斯蒂芬周的博客中有“本文隐藏内容登录后才可以查看”,于是想也有这样的功能,但是只要变成回复就可以查看,百度了很多内容,发现都不太行,插件好像也不大行,岁月小筑的代码发生经过测试好像是可以的。

wordpress代码:在文章中设置回复可见内容

1)在functions.php文件中插入如下代码:

/*回复可见Functions*/
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 = "public@xjh.me"; //博主自己的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');

2)插入短代码

wordpress代码:在文章中设置回复可见内容

[reply]欢迎大家来的我的网站,以后会分享一些有用的视频和文章[/reply]

ps:发现一定得先注册才行呀!不然回复无效。

    (0)
    打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
    xujunzju管理者
    上一篇 2019年12月15日 00:54
    下一篇 2020年4月4日 12:22

    发表回复

    登录后才能评论

    评论列表(3条)

    • 匿名 2019年12月30日 00:28

      我想看是否是成功了

    • test2 2019年12月30日 01:05

      再试一次

    • 匿名 2019年12月30日 02:02

      可以评论吗

    联系我们
    邮箱:
    xujunzju@gmail.com
    公众号:
    xujunzju6174
    捐赠本站
    捐赠本站
    分享本页
    返回顶部