前不久,我们介绍了另外一篇Wordpress内容加密的文章,【WordPress内容加密,文章实现部分内容可见或隐藏,输入密码后可见】,这一期的Wordpress内容加密我们来介绍下【用户评论后内容可见】,实现方法如下:
一,在你网站正在使用的主题functions.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)); //自定义评论内容加样式 $pos = strpos($notice, 'reply-to-read'); if ($pos === false) { $notice = '<p class="reply-to-read"><a href="#respond" title="评论本文">'.$notice.'</a></p>'; } $email = null; $user_ID = (int) wp_get_current_user()->ID; if ($user_ID > 0) { $email = get_userdata($user_ID)->user_email; //对站长直接显示内容 $admin_email = "568508200@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');
二,在主题style.css样式文件里添加样式[可选]
p.reply-to-read{padding:15px 0; font-size:16px; color:#3897f0;}
三,调用方法
//1默认方法 [reply]评论可见的内容[/reply] //2定制方法 [reply notice="自定义的提示信息"]评论可见的内容[/reply]
四,测试功能,下面又隐藏内容了一份【福利】,你可以测试看看是否可行?!
五,文章主题之外的记录,wordpress分页标签
<!--nextpage-->
相关文件推荐阅读:
WordPress内容加密,文章实现部分内容可见或隐藏,输入密码后可见
WordPress内容加密,用户评论后内容可见+分页标签
WordPress插件发布,隐藏内容登陆后可见