WordPress插件发布:隐藏内容登陆后可见
请不要在登录状态下测试隐藏效果!
应 流年 网友的请求,特地研究了一下在WordPress博客中,如何实现隐藏文章的部分内容,使得用户在登陆后才可以看到。其实这个难度不是很大,WordPress提供了很多的编程接口,可以很方便的实现很多功能,现在我使用WordPress Shortcode API,也就是简码,做了一个插件,基本上可以满足文章隐藏内容,登陆后可见的需求。之前也有人开发过相关的插件,不过那是隐藏文章全部内容,不能做到只隐藏部分内容,可以说今天我做的这个插件更加灵活一些。
1.0版推出后不久,网友yken89提了一个非常有建设意义的意见。2.0版本采纳该网友的建议,添加了新的标签
<!--loginview start-->hidden contents.<!--loginview end-->
并在HTML模式下,添加了loginview按钮,方便用户添加隐藏内容。2.0版借鉴了WordPress插件Easy2hide的部分代码。升级至2.0版后,原来的标签[loginview]仍然有效。3.0版本修正了对WordPress 3.3的支持。
名称:Login to view all
版本:3.1
作者:Ludou
更新:2013-12-20
功能:您可以隐藏文章的部分内容,使得用户必须登录,才能浏览。
下载:WordPress 官方下载,本地下载
WordPress最低版本要求:WordPress 2.7 及更高版本
安装方法:
1. 下载插件,解压缩,你将会看到一个文件夹login-to-view-all,然后将其放置到插件目录下,插件目录通常是 `wp-content/plugins/`
2. 在后台对应的插件管理页激活该插件Login to view all
4. 完成
使用说明:
1. 在WordPress后台编辑文章的时候,切换到HTML模式,选中你要隐藏的内容,点击按钮 loginview 即可用
<!--loginview start-->
和
<!--loginview end-->
将隐藏内容括起来;使用这个标签的好处是,你停用本插件后,该标签不会被显示出来。
2. 如果你不喜欢HTML代码模式,可以使用 [loginview] 和 [/loginview] 将你想要隐藏的内容括起来,该标签支持可视化模式和HTML编辑模式。你的文章内容应该像这样子:
[loginview]这里是你要隐藏的内容[/loginview]
3. 这样未登录的用户浏览文章的时候,将无法阅读隐藏的内容。
效果预览图:
Plugin name:Login to view all
Version: 3.0
Author: Ludou
Update: 2012-02-11
Description:Login to view all is an plugin designed to help you add hidden contents of your post. The hidden contents are only visible for the visitor who are logged in.
Download: From wordpress.org
Installation:
- Upload to your plugins folder, usually `wp-content/plugins/` and unzip the file, it will create a `wp-content/plugins/login-to-view-all/` directory.
- Activate the plugin on the plugin screen.
- Done
How to use?
1. You can switch to HTML editor and click the "loginview" button, to put your words needed to hide between "<!--loginview start-->" and "<!--loginview end-->". like:
<!--loginview start-->hidden contents.<!--loginview end-->
This has the advantage of that if you disable this plugin, the tag <!--loginview start--> and <!--loginview end--> will still not be displayed.
2. Or you can use it like that in your blog post :
[loginview]hidden contents.[/loginview]
[loginview] can be use in the visual editor and HTML editor.
Screenshots
-- 完 --
本文采用 「CC BY-NC-SA 4.0」创作共享协议,转载请标注以下信息:
原文出处:露兜即刻 https://www.ludou.org/wordpress-plugin-login-to-view-all.html
请问博主,怎么才能显示英文(you must be lugged to view the hidden contents)呢?默认的貌似是中文呀
@young 用英文版的wordpress。
或者login-to-view-all.php,把里面的所有,类似:
__('xxxx', 'ludouview')
改成 'xxxx'
很不错的插件!其他插件都是隐藏文章全部内容,无法隐藏部分内容。这个插件正好能隐藏任意内容,刚好可以满足我的下载需求。O(∩_∩)O谢谢分享!
博主好东西呀~ 万分感谢!
博主能不能做成 “点击按钮可见内容” 呢。O(∩_∩)O~~
@囧囧小男纸 抱歉,暂时没这方面的想法
Hi Ludou,
Thanks for great plugin. But when i've tried to hide photos, this is not work. Do you know how to use for photo?
@Leo hei, I've tried to hide photos and it' OK.
You can switch to HTML editor and click the "loginview" button, to put your words needed to hide between "<!–loginview start–>" and "<!–loginview end–>".
With img tag,like:
<!–loginview start–>hidden contents.<a href="h ttp://abc.com/wp-content/uploads/abc.jpg"><img class="aligncenter size-full wp-image-1220" title="abc" src="h ttp://abc.com/wp-content/uploads/abc.jpg" alt="" width="502" height="30" /></a>
<!–loginview end–>
这个功能好啊,正在找。
其实我还想,如果能设置回复才能看到文章全部内容就更好了。
@广东通讯 easy2hide
@Ludou 要是这个插件能够在wlw里也能用该多好啊?要求是不是过了点?
博主能不能再添加个注册的功能呢?类似于dz论坛的,效果就是:您需要 登录 才可以查看,没有帐号?注册
@张士超 开放注册后,登录界面有注册链接。
@Ludou 是啊,可是这个注册之后返不回注册之前的页面,本人菜鸟求指点
@张士超 WordPress没有注册后返回的功能!
文本编辑器打开login-to-view-all.php,查找:
__('You must be ', 'ludouview') . '<a href="' . wp_login_url( get_permalink() ) . '">' . __('logged in', 'ludouview') . '</a>' . __(' to view the hidden contents.', 'ludouview')
改成:
. '您需要 <a href="' . wp_login_url( get_permalink() ) . '">登录</a> 才可以查看,没有帐号?<a href="http:// 这里填注册页面网址">注册</a>'
UTF-8编码另存为覆盖原来的文件,否则乱码!
@Ludou 好的,谢谢
博主您好,
请问我用了社会化登陆(登鹭) , 我用了您的插件,访问者 点击社会化登陆框(在文章最下评论框上方) 用QQ等登陆就能看到 隐藏的东西,但是我想 用社会化登陆的同时 实现 回复才能显示,请问怎么弄。。。 另外,如果这样不可以的话 ,我想实现 点击 您设计的 登陆 可见的 登陆字样,自动调用 社会化登陆的登陆框 要怎么做? 或者直接 点击登陆的时候 网页自动 转到 评论上方社会化登陆 ,,,请问如何实现。 谢谢
@iamfine.cn 暂时没兴趣去研究登鹭的代码
@Ludou 博主您好,我的站www.iamfine.cn ,昨天有问过您关于登鹭代码的问题, 我现在想请教您一个问题 就是 如何 点击 <a href="http:// 这里填注册页面网址">注册</a>'这里面注册文字的时候 去调用一段 JS 代码,实现 JS 弹出 的效果。
另外 我写了一篇文章 http://www.iamfine.cn/archives/231 表明我的意思,
请帮忙查看!
万分感谢您能从百忙中看我回复!
感谢!
@iamfine.cn 如果以下代码不行,请不要再向我提问此问题:
文本编辑器打开login-to-view-all.php,查找:
__('You must be ', 'ludouview') . '<a href="' . wp_login_url( get_permalink() ) . '">' . __('logged in', 'ludouview') . '</a>' . __(' to view the hidden contents.', 'ludouview')
改成:
. '您需要 <a onclick="javascript:_CAC.intoLogin(true);" href="javascript:;" class="dl_media_more" hidefocus="true" rel="nofollow">登录</a> 才可以查看,没有帐号?<a href="http:// 这里填注册页面网址">注册</a>'
@Ludou 谢谢,
我想做个收费博客,你的插件应该是我找的。但下了你插件后不能用, 是版本的问题吗 我用3.4.2的wordpress.
@走走强 请先退出你的账户,再看效果!
@Ludou 我看到效果了。很好。但不是用户注册然后我让他们缴费之类的,是登录wordpress的后台。是吧。
博主,怎么集成到主题文件,而不是使用插件方式?对插件无爱啊
@fukr login-to-view-all.php 第18-35行,复制到functions.php中,编辑文章的时候,使用 loginview start 那种标签把要隐藏的内容括起来
@Ludou 谢谢博主,经测试有效,很强大,一个函数就搞定了!
@fukr 哇哇哇!看到这个我很激动!!!我也正在寻找这个方法,赞一个先。
无名以来,只想说声感谢分享…
请问博主这个插件能很好兼容3.5*版本的wp吗?
@老刘 暂时没发现什么问题。
給力
easy2hide不支持第三方评论啊,露兜大哥有空帮忙改下吧
@历史的天空 那去找第三方评论的技术支持吧,具体运行机制暂时没兴趣去研究。
在3.5.1版本中没有loginview 按钮,您能修改一下吗?
@zz 请切换到文本模式
@Ludou 文本模式下也没有,我用的是英文的wp
wordpress能做出这样的功能博主实属不易,看了评论,大家想要的功能,我已经用织梦dedecms做出来了,wordpress现在缺的就是会员系统,没有良好的后台支持,这个功能发展堪忧。博主你的google ad不这样放置不会被google警号?google好像不允许滑动展示ad的。
@atomhat 好吧,您在哪里看到google有这样的条款呢?
我们鼓励发布商尝试各种展示位置和广告格式。但是,发布商不能将 AdSense 代码放置在弹出式窗口、电子邮件或软件等不当位置中。另外,发布商还必须遵守与所使用的各产品相对应的合作规范。
Google 广告、搜索框或搜索结果不得:
集成到包括工具栏在内的任何类型的软件应用之中(不适用于 AdMob)。
在弹出式窗口或背后弹出式窗口中显示。
放置在电子邮件、电子邮件程序(包括网络邮件)内或主要由动态内容(如实时聊天、即时通讯或自动刷新的评论)构成的网页上。(不适用于 AdMob。)
放置在电子邮件、电子邮件程序或聊天程序内。(不适用于 AdMob。)
被网页上的元素隐藏。
放置在任何非内容网页上。(不适用于 AdSense 搜索广告、AdSense 移动搜索广告和 AdMob。)
放置在专门为展示广告而发布的网页上。
放置在具有以下特点的网页上:由于徽标、商标或其他品牌特征的不当使用,其内容或网址可能会让用户误以为该网页与 Google 有关。
采用违反其他 Google 产品或服务政策的方式,放置在相关产品或服务的上方、内部或侧面
@atomhat 刚刚看了Google的合作规范,这种广告展示方式确实是不行的:
https://support.google.com/adsense/answer/3394713?hl=zh-Hans&ref_topic=1250104#2
虽然可以隐藏成功,但其实浏览器查看源文件还是可以看到隐藏的内容~
@打副本酱 退出登录再看看吧
博主什么时候更新插件呢?官网显示超过2年没更新了
@Mo 好吧,更新些什么呢?就这么单一的功能。
难道在新版本的WordPress中用不了了?
@Ludou 怪不得不更新,我以为是没人管了,原来是不需要。
在3.6.1里面还是完全有效,非常感谢博主。
@Ludou 博主,请问代码要加进哪一个文件夹下的functions.php啊?我加进目前使用的主题,这段代码能够隐藏内容,但是登录前后Wordpress都会报错,而且登录也会出问题。
Warning: Cannot modify header information – headers already sent by (output started at …\wp-content\themes\responsive\functions.php:24) in …\wordpress\wp-login.php on line 384
还有\wordpress\wp-includes\pluggable.php也被影响到了。
第24行就是代码的开始。
@悠悠 不知道你收的是哪些代码
@Ludou 就是上面有个朋友问的怎么整合到主题文件里面你所说的代码。
login-to-view-all.php 第18-35行,复制到functions.php中,编辑文章的时候,使用 loginview start 那种标签把要隐藏的内容括起来。
这一段。
@悠悠 那看看你22-25行的代码是怎么写的,复制过了看看。
@Ludou 博主你好,我已经把18-35行的代码加到主题\responsive\core\includes\function.php里面了。
这一段上面的代码是
/*
* Globalize Theme options
*/
$responsive_options = responsive_get_options();
下面的代码是
/**
* Add plugin automation file
*/
require_once( dirname( __FILE__ ) . '/classes/class-tgm-plugin-activation.php' );
目前操作没有发生什么问题了,我之前出错的时候好像是加在$responsive_options = responsive_get_options();这一段的前面。请问博主,我现在加的位置是可行的吗?
@悠悠 没有问题。确保跟其他代码不在同一行即可,不然容易混淆。
谢谢分享这个插件,效果非常不错!隐藏的内容不登录确实无法接收到,不过搜索的时候会搜到,如果隐藏的内容不登陆无法搜索到就更好了!
露兜,谢谢你的分享。现在我遇到一点问题希望你能抽时间解答:
我在插件前面加了一个限制条件
global $current_user;
get_currentuserinfo();
if(is_user_logged_in() && $current_user->user_level > 1)
目的是只让大于1的用户查看隐藏内容,现在的问题是等级1的用户登录以后再点这个链接出现的是登录界面,我的想法是如果没有登录的用户点击是出现登录界面,等级1的用户点击出现另外一个站内链接,怎么写?
另外我在网上找到了一段代码,麻烦看看。
<?php
function redirect_user_on_role()
{
//retrieve current user info
global $current_user;
get_currentuserinfo();
//If login user role is Subscriber
else if ($current_user->user_level == 0)
{
wp_redirect( home_url() ); exit;
}
//If login user role is Contributor
else if ($current_user->user_level > 1)
{
wp_redirect( home_url() ); exit;
}
//If login user role is Editor
else if ($current_user->user_level >8)
{
wp_redirect( home_url() ); exit;
}
// For other rolse
else
{
$redirect_to = 'http://google.com/';
return $redirect_to;
}
}
add_action('admin_init','redirect_user_on_role');
?>
这段代码我也想加入进来,如果没有登录的用户看到文章点击后可以按用户角色进入不同界面。
等级大于1的返回先前页面,小于1的跳转到站内某一界面,管理员进后台。
但是我改好之后不管在加在functions.php的哪一个位置都显示出错,我相信代码没错是我方法错误,
还希望兜哥能给我一些解答?或者能不能把我这几个条件合并起来呢?
@瘦猴 你的描述有问题:如果没有登录的用户看到文章点击后可以按用户角色进入不同界面
既然都没有登录,如何知道他的角色?
下面是我修改后的代码,跳转到哪个页面自己改:
function redirect_user_on_role() {
if(!empty($_GET['action']) && $_GET['action'] != 'login')
return;
//retrieve current user info
global $current_user;
get_currentuserinfo();
//If login user role is Subscriber
if ($current_user->user_level == 0) {
wp_redirect( home_url() );
exit;
}
//If login user role is Contributor
else if ($current_user->user_level > 1) {
wp_redirect( home_url() );
exit;
}
//If login user role is Editor
else if ($current_user->user_level > 8) {
wp_redirect( home_url() );
exit;
}
// For other rolse
else {
wp_redirect( 'http://google.com/' );
exit;
}
}
add_action( 'login_init', 'redirect_user_on_role' );
@Ludou 露兜 可能是我的语言没有组织好 , 我的意思是用户登录以后进入不同的站内不同的页面,管理进后台,用户等级大于1的返回首页,等于小于1的回站内某一页面。
同时我又在你的评论中找到了另外一组代码,
function my_login_redirect($redirect_to, $request, $user){
if( is_array( $user->roles ) ) {
// 检查用户的角色
if( in_array( "administrator", $user->roles ) ) {
// 跳转到管理员用户界面
return $redirect_to;
} else {
// 跳转到其他用户界面
return home_url();
}
}
}
add_filter("login_redirect", "my_login_redirect", 10, 3);
但是里面只有两种选择,我加了之后就错误,自己没学过代码,所以很多只能百度,还希望露兜能抽点时间帮我改一下。
用户只有3种:Subscriber Contributor Administrator
另外我的第一个问题能帮我解决一下嘛?
我在插件前面加了一个限制条件
global $current_user;
get_currentuserinfo();
if(is_user_logged_in() && $current_user->user_level > 1)
我的想法是只让大于1的用户查看隐藏内容,等级小于等于1的用户点击出现另外一个站内链接,怎么写?
谢谢了。
@瘦猴 function my_login_redirect($redirect_to, $request, $user){
global $user;
if(isset( $user->roles ) && is_array( $user->roles ) ) {
// 检查用户的角色
if( in_array( "administrator", $user->roles ) ) {
// 管理员跳转,网址自己改
return 'http://';
}
elseif( in_array( "contributor", $user->roles ) ) {
// 跳转到投稿者页面
return 'http://';
}
elseif( in_array( "subscriber", $user->roles ) ) {
// 跳转到管订阅者页面
return 'http://';
}
else {
// 跳转到其他用户界面
return 'http://';
}
}
}
add_filter("login_redirect", "my_login_redirect", 10, 3);
@Ludou 非常感谢,关于我的第二个问题就是插件的问题还要麻烦你一下。
add_filter('the_content', 'login_to_view');
function login_to_view($content)
{
if (preg_match_all('/<!–loginview start–>([\s\S]*?)<!–loginview end–>/i', $content, $hide_words))
{
global $current_user;
get_currentuserinfo();
if(is_user_logged_in() && $current_user->user_level > 1)
{
$content = str_replace($hide_words[0], $hide_words[1], $content);
}
else
{
$hide_notice = '<div style="text-align:center;border:1px dashed #FF9A9A;padding:8px;margin:10px auto;color:#FF6666;">' . __('You must be ', 'ludouview') . '<a href="' . wp_login_url( get_permalink() ) . '">' . __('logged in', 'ludouview') . '</a>' . __(' to view the hidden contents.', 'ludouview') . '</div>';
$content = str_replace($hide_words[0], $hide_notice, $content);
}
}
return $content;
}
我的想法是
如果用户已登录那么只让大于1的用户查看隐藏内容,等级小于等于1的用户继续隐藏内容并显示需要提升用户等级。
如果用户未登录,不管是什么等级都是隐藏的
麻烦了,多谢。
@瘦猴 用户未登录无法判断
@Ludou 这条是回复我的吗?
我只需要登录后的判断就行了。
如果用户已登录那么只让大于1的用户查看隐藏内容,等级小于等于1的用户继续隐藏内容并显示需要提升用户等级。
@瘦猴 $hide_notice = '<div style="text-align:center;border:1px dashed #FF9A9A;padding:8px;margin:10px auto;color:#FF6666;">' . __('You must be ', 'ludouview') . '<a href="' . wp_login_url( get_permalink() ) . '">' . __('logged in', 'ludouview') . '</a>' . __(' to view the hidden contents.', 'ludouview') . '</div>';
改成:
$hide_notice = '<div style="text-align:center;border:1px dashed #FF9A9A;padding:8px;margin:10px auto;color:#FF6666;">您的用户等级还不够或者未登录!点此 <a href="' . wp_login_url( get_permalink() ) . '">登录</a></div>';
@Ludou 如果想要让已登录用户和未登录用户显示不同的提示语呢?是否前面又要加一个判断?还是前面回复的判断不了?
3.8的wordpress 在编辑器哪里没有了按钮了。。求更新。。
@艳文 按钮还在的哦,在文本模式可以看到的哦。
请问博主在哪里修改“本文隐藏内容 登陆 后才可以浏览”这些文字?
@sacds 文本编辑器打开login-to-view-all.php,查找:
__('You must be ', 'ludouview') . '<a href="' . wp_login_url( get_permalink() ) . '">' . __('logged in', 'ludouview') . '</a>' . __(' to view the hidden contents.', 'ludouview')
翻译其中的英文
You must be logged in to view the hidden contents.
1. 登陆 二字有误,应为 登录。
2. 上面有人提的 点击按钮查看内容 其实也有一定的用途,比如有些内容不方便被RSS收录器抓取——有可能导致内容不当而被封杀等,但又不想增加无谓的过路注册用户,可以免除不少垃圾。
博主你好,十分感谢提供这么好的插件,但是应用中却不能把内容隐藏,查看源码也没有问题,不知道哪里的问题,望赐教,谢谢。
<!–loginview start–>
<p align="left">CAREMET TRAVEL </p>
<!–loginview end–>
@audience 不好意思,应用错了, 这个是管理文章的,我用在了新建页面中不好用,不知道有没有是否可以通用?
神插件.
很好用.
这个屌.