在很多网站上都看过这个效果,比如说知己知彼网站,他的部分资源是需要我们评论后才能下载的,那么这个到底有什么用呢,对我而言,除了拿来装逼,还可以增加我的评论数量,不多说,先看看效果:

其实WordPress有很多的插件可以实现这个功能,比如说Easy2Hide,但是插件当然是越少越好,下面我就来说说怎么用代码实现这个功能:

网上的原文:
在当前主题的functions.php文件添加以下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
function reply_to_read($atts, $content=null) {
    extract(shortcode_atts(array("notice" => '<p class="reply-to-read" style="border-width: 1px 1px 1px 1px;border-color: #F2F2F2;line-height: 150%;"><blockquote><font color="#ff0000"><b>温馨提示</b></font>: 隐藏内容需要<a href="#respond" title="点击进行评论"> 回复评论 </a>后才能查看, 评论后请 <strong><a href="javascript:location.reload()" title="点击刷新"> 刷新 !</a></strong>.</blockquote></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@boke8.net"; //把左面的邮箱换成博主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');

注:把代码中的“xxx@boke8.net”换成博主邮箱地址

我修改后的代码:
我开始直接用的网上的代码,但是发现显示出来的效果有错误,于是自己修改了一下标签和样式,然后修改了自己的邮箱号码。其他部分的代码并不需要修改。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
function reply_to_read($atts, $content=null) {
    extract(shortcode_atts(array("notice" => '<p class="reply-to-read" style="text-align:center; border:2px solid #f00; border-style:dotted; border-radius:4px; padding:5px; margin:10px;"><strong style="color: red;">温馨提示:</strong>为了避免资源链接被和谐,此处内容需要您<strong><a href="#respond" title="点击进行评论"> 回复评论 </a></strong>后才能查看, 评论后请 <strong><a href="javascript:location.reload()" title="点击刷新"> 刷新!</a></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 = "970852638@qq.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');

这样就可以让别人只有回复了评论才能下载你网站资源的效果,是不是很有逼格,我今天刚使用了这个就有了新的评论呢。
参考网址:http://www.boke8.net/the-wp-post-visible-when-comments.html

纯代码实现wordpress文章隐藏内容评论可见的更多相关文章

  1. 纯代码实现WordPress评论回复自动添加@评论者的功能

    先看看效果: 这个有什么用呢?添加了@功能之后那些用户评论之间的层次关系就很清晰了,我们可以清楚地知道这些评论是谁发给谁的. 其实主要是为了提升逼格. 实现方法: 将下面代码加入function.ph ...

  2. 纯代码实现WordPress上传图片自动重命名的方法

    在我们使用 WordPress 发布文章时,经常都需要添加图片.多媒体什么的.然而,大家都知道 WordPress 是舶来物,对于中文用户来说,我们都会把图片命名为中文的,由于 WordPress 机 ...

  3. 纯代码实现wordpress图片本地化【5.2.2版本可用】

    最近用wordpress做了个网站,想复制些新闻过来用,但图片是别人的,就想怎么本地化,在网找到了方法,那就保存一下方法. 复制下面的代码,然后粘贴到你当前WordPress主题的模版函数(funct ...

  4. Wordpress 之删除 RSS 功能 的"文章RSS"、"评论RSS"、"WordPress.org"

    一. 去除底部“自豪地采用 WordPress”版权信息: 1.打开主题文件夹:wp-content/themes/twentyeleven/footer.php; 2.找到 这段代码 删除即可: & ...

  5. wordpress 获取指定作者或者文章的所有评论数量

    wordpress 获取指定作者或者文章的所有评论数量 <?php $args = array( 'post_author' => '' // fill in post author ID ...

  6. iOS纯代码手动适配 分类: ios技术 2015-05-04 17:14 239人阅读 评论(0) 收藏

    首先说下让自己的程序支持iPhone6和6+,第一种使用官方提供的launch screen.xib,这个直接看官方文档即可,这里不再多述:第二种方法是和之前iPhone5的类似,比较简单,为iPho ...

  7. 潭州课堂25班:Ph201805201 django 项目 第二十四课 文章主页 多级评论数据库设计 ,后台代码完成 (课堂笔记)

    加载新闻评论功能 1.分析 业务处理流程: 判断前端传的新闻id是否为空,是否为整数.是否不存在 请求方法:GET url定义:'/news/<int:news_id>' 请求参数:url ...

  8. 怎么用代码制作WordPress的归档页面

    先看看效果,这个是我网站的归档页面:http://www.shenjieblog.com/archives 其实WordPress自带了一个归档的功能,但是只能显示在网页中的某一个部分,但是我想单独制 ...

  9. WordPress REST API 内容注入漏洞

    1 WordPress REST API 内容注入漏洞 1.1 摘要 1.1.1 漏洞介绍 WordPress是一个以PHP和MySQL为平台的自由开源的博客软件和内容管理系统.在4.7.0版本后,R ...

随机推荐

  1. CentOS7上安装并配置Nginx、PHP、MySql

    一.Nginx 1.安装nginx yum install nginx 2.启动nginx systemctl start nginx 除了systemctl start nginx之外,常用的相关命 ...

  2. 1.8 range

    哈哈,前边忘了介绍这个知识点了,老是用人家,不介绍一下都不好意思了. range()函数是一个用来创建数字序列的函数. 问题来了,为什么要写函数? 封装代码啊,让使用者不需要关心具体业务逻辑是如何实现 ...

  3. 有状态InheritableThreadLocal 配合 JDK8 ,异步方法调用

    我们可以把一个类的作用域注解为 @Scope(scopeName = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode. ...

  4. Java常见加密算法

    常见 package com.example.decript; import java.io.UnsupportedEncodingException; import java.security.In ...

  5. FusionCharts封装-Data

    DataSet.java: /** * @Title:DataSet.java * @Package:com.fusionchart.model * @Description:FusionCharts ...

  6. 个性化WinPE封装方法----制作过程需要了解的“命令”

    1.在现有的Windows7条件下,自动在E盘建立mywinpe文件夹,设置 Windows PE 构建环境,并保存到E:\mywinpe下 copype.cmd x86 E:\mywinpe 2.将 ...

  7. MyEclipse报错

    MyEclipse报错

  8. Java代码输出是“father”还是“child”(二)

    1.实例 /** * 以下代码输出的结果是 */ package com.you.model; /** * @author YouHaidong * 输出的结果 */ public class Fat ...

  9. but the supplied types were (flex.messaging.io.amf.ASObject) and converted to (null)."

    1.错误描述 [RPC Fault faultString="Cannot invoke method  'saveOrUpdate'. " faultCode="Ser ...

  10. 获取对固定列不重复的新DataTable

    获取对固定列不重复的新DataTable  ///<summary> /// 获取对固定列不重复的新DataTable /// </summary> ///<param ...