
相信很多同学在一些博客当中看到有些内容需要登录后、回复后或输入密码后才能看到,是不是觉得很有意思,可以相对的增加与访客的互动,特别是加密后输入密码可见可以上部分内容只针对特点人群开放。在此文章中蜗牛将为大家分享如何通过插件或代码来隐藏WordPress文章部分内容,让用户登录或输入密码后才能看见。
一、用插件方法实现
首先推荐一款小巧的插件:Login to view all,来实现使隐藏WordPress文章部分内容,让用户登录后可见。用插件的还出是简单便捷,只需下载插件并上传安装好启用即可。
1、百度网盘下载
2、WordPress官方下载:https://wordpress.org/plugins/login-to-view-all/
关于插件的使用及效果,就不做演示了,大家自己体验。
插件版优缺点:
使用插件版后,如果不想使用插件了,我们屏蔽插件后,原先文章内隐藏的内容会直接消失掉。这个缺点很致命,要用此插件就要决定长期使用下去。优点是与CDN加速兼容性较好,不会存在登录不显示内容的现象。
方二、修改代码方式实现
个人更倾向于使用代码,这里为大家介绍修改代码实现回复可见、登录可见或输入密码可见三种方式的修改操作。与插件版相比,代码版优缺点也是各半,大家自己斟酌选择。
代码版优缺点:
优点是与插件版相比,我们不想使用此功能时,取消相应代码,原先隐藏的内容会正常显示。缺点是与CDN加速兼容较差,会被缓存,如果你的站点开启了CDN加速,会出现登录也无法显示内容的情况。
1、简单版部分内容登录可见
在主题的functions.php文件添加以下代码:
<span class="hljs-comment">//部分内容登录可见</span>add_shortcode(<span class="hljs-string">'hide'</span>,<span class="hljs-string">'loginvisible'</span>);<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">loginvisible</span><span class="hljs-params">($atts,$content=null)</span></span>{ <span class="hljs-keyword">if</span>(is_user_logged_in() && !is_null($content) && !is_feed()) <span class="hljs-keyword">return</span> $content; <span class="hljs-keyword">return</span> <span class="hljs-string">''</span>;}<span class="hljs-comment">//部分内容登录可见</span>
add_shortcode(<span class="hljs-string">'hide'</span>,<span class="hljs-string">'loginvisible'</span>);
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">loginvisible</span><span class="hljs-params">($atts,$content=null)</span></span>{
<span class="hljs-keyword">if</span>(is_user_logged_in() && !is_null($content) && !is_feed())
<span class="hljs-keyword">return</span> $content;
<span class="hljs-keyword">return</span> <span class="hljs-string">''</span>;
}
//部分内容登录可见
add_shortcode('hide','loginvisible');
function loginvisible($atts,$content=null){
if(is_user_logged_in() && !is_null($content) && !is_feed())
return $content;
return '';
}
如何实现?在编辑文章是使用短码包围要隐藏的内容,如下:
【hide】登陆才可以看到的内容【/hide】【hide】登陆才可以看到的内容【/hide】【hide】登陆才可以看到的内容【/hide】
把上面中文括号改为英文括号【】→[]
稍微会点代码的其实都挺简单的!
2、美化版部分内容登录可见
在主题function.php文件里加入以下代码。其中可用于直接将href=”#respond”后的“#respond”替换为自己站点的登录地址,以方便用户快速登录。
<span class="hljs-comment">//部分内容登录可见</span><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">login_to_read</span><span class="hljs-params">($atts, $content=null)</span> </span>{extract(shortcode_atts(<span class="hljs-keyword">array</span>(<span class="hljs-string">"notice"</span> => <span class="hljs-string">'</span><span class="hljs-string">温馨提示:</span><span class="hljs-string">此处内容需要</span><a title="登录后可见" href="https://labishaxin.com/?golink=aHR0cHM6Ly93d3cuY2hlbmJvLmluZm8vZ28vSTNKbGMzQnZibVE9"><span class="hljs-string">登录</span></a><span class="hljs-string">后才能查看!'</span>), $atts));<span class="hljs-keyword">if</span> ( is_user_logged_in() && !is_null( $content ) && !is_feed() ) <span class="hljs-keyword">return</span> $content; <span class="hljs-keyword">return</span> $notice;}add_shortcode(<span class="hljs-string">'vip'</span>, <span class="hljs-string">'login_to_read'</span>);<span class="hljs-comment">//部分内容登录可见</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">login_to_read</span><span class="hljs-params">($atts, $content=null)</span> </span>{
extract(shortcode_atts(<span class="hljs-keyword">array</span>(<span class="hljs-string">"notice"</span> => <span class="hljs-string">'
</span><span class="hljs-string">温馨提示:</span><span class="hljs-string">此处内容需要</span><a title="登录后可见" href="https://labishaxin.com/?golink=aHR0cHM6Ly93d3cuY2hlbmJvLmluZm8vZ28vSTNKbGMzQnZibVE9"><span class="hljs-string">登录</span></a><span class="hljs-string">后才能查看!
'</span>), $atts));
<span class="hljs-keyword">if</span> ( is_user_logged_in() && !is_null( $content ) && !is_feed() )
<span class="hljs-keyword">return</span> $content;
<span class="hljs-keyword">return</span> $notice;
}
add_shortcode(<span class="hljs-string">'vip'</span>, <span class="hljs-string">'login_to_read'</span>);
//部分内容登录可见
function login_to_read($atts, $content=null) {
extract(shortcode_atts(array("notice" => '
温馨提示:此处内容需要登录后才能查看!
'), $atts));
if ( is_user_logged_in() && !is_null( $content ) && !is_feed() )
return $content;
return $notice;
}
add_shortcode('vip', 'login_to_read');
如何实现?在编辑文章是使用短码包围要隐藏的内容,下面列举的是两种方式,我们任选一种即可。
【vip]我是被隐藏的内容,样式一(默认样式)【/vip】【vip notice=<span class="hljs-string">"登录后才显示哟"</span>]我是被隐藏的内容,样式二(自定义回复信息)【/vip】【vip]我是被隐藏的内容,样式一(默认样式)【/vip】
【vip notice=<span class="hljs-string">"登录后才显示哟"</span>]我是被隐藏的内容,样式二(自定义回复信息)【/vip】
【vip]我是被隐藏的内容,样式一(默认样式)【/vip】
【vip notice="登录后才显示哟"]我是被隐藏的内容,样式二(自定义回复信息)【/vip】
把上面中文括号改为英文括号【】→[]
本站使用内容登录可见示例
温馨提示:此处内容需要登录后才能查看!
3、输入密码显示
首先在主题functions.php文件中添加下面代码。
<span class="hljs-comment">//部分内容输入密码可见 </span><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">e_secret</span><span class="hljs-params">($atts, $content=null)</span></span>{ extract(shortcode_atts(<span class="hljs-keyword">array</span>(<span class="hljs-string">'key'</span>=><span class="hljs-keyword">null</span>), $atts));<span class="hljs-keyword">if</span>(<span class="hljs-keyword">isset</span>($_POST[<span class="hljs-string">'e_secret_key'</span>]) && $_POST[<span class="hljs-string">'e_secret_key'</span>]==$key){ <span class="hljs-keyword">return</span> <span class="hljs-string">'<div class="e-secret">'</span>.$content.<span class="hljs-string">'</div>'</span>; }<span class="hljs-keyword">else</span>{ <span class="hljs-keyword">return</span> <span class="hljs-string">'<form class="e-secret" action="'</span>.get_permalink().<span class="hljs-string">'" method="post" name="e-secret"><label>输入密码查看加密内容:</label><input type="password" name="e_secret_key" class="euc-y-i" maxlength="50"><input type="submit" class="euc-y-s" value="确定"><div class="euc-clear"></div></form>'</span>; }} add_shortcode(<span class="hljs-string">'secret'</span>,<span class="hljs-string">'e_secret'</span>);<span class="hljs-comment">//部分内容输入密码可见 </span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">e_secret</span><span class="hljs-params">($atts, $content=null)</span></span>{ extract(shortcode_atts(<span class="hljs-keyword">array</span>(<span class="hljs-string">'key'</span>=><span class="hljs-keyword">null</span>), $atts));
<span class="hljs-keyword">if</span>(<span class="hljs-keyword">isset</span>($_POST[<span class="hljs-string">'e_secret_key'</span>]) && $_POST[<span class="hljs-string">'e_secret_key'</span>]==$key){
<span class="hljs-keyword">return</span> <span class="hljs-string">'<div class="e-secret">'</span>.$content.<span class="hljs-string">'</div>'</span>;
}
<span class="hljs-keyword">else</span>{
<span class="hljs-keyword">return</span> <span class="hljs-string">'<form class="e-secret" action="'</span>.get_permalink().<span class="hljs-string">'" method="post" name="e-secret"><label>输入密码查看加密内容:</label><input type="password" name="e_secret_key" class="euc-y-i" maxlength="50"><input type="submit" class="euc-y-s" value="确定"><div class="euc-clear"></div></form>'</span>;
}
}
add_shortcode(<span class="hljs-string">'secret'</span>,<span class="hljs-string">'e_secret'</span>);
//部分内容输入密码可见
function e_secret($atts, $content=null){ extract(shortcode_atts(array('key'=>null), $atts));
if(isset($_POST['e_secret_key']) && $_POST['e_secret_key']==$key){
return '<div class="e-secret">'.$content.'</div>';
}
else{
return '<form class="e-secret" action="'.get_permalink().'" method="post" name="e-secret"><label>输入密码查看加密内容:</label><input type="password" name="e_secret_key" class="euc-y-i" maxlength="50"><input type="submit" class="euc-y-s" value="确定"><div class="euc-clear"></div></form>';
}
}
add_shortcode('secret','e_secret');
第二步到在自己主题main.css样式文件里添加下面代码。
<span class="hljs-comment">/*e-secret*/</span><span class="hljs-selector-class">.e-secret</span> { <span class="hljs-attribute">margin</span>: <span class="hljs-number">20px</span> <span class="hljs-number">0</span>; <span class="hljs-attribute">padding</span>: <span class="hljs-number">20px</span>; <span class="hljs-attribute">background</span>: <span class="hljs-number">#f8f8f8</span>;}<span class="hljs-selector-class">.e-secret</span> <span class="hljs-selector-tag">input</span><span class="hljs-selector-class">.euc-y-i</span><span class="hljs-selector-attr">[type="password"]</span> { <span class="hljs-attribute">float</span>: left; <span class="hljs-attribute">background</span>: <span class="hljs-number">#fff</span>; <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>; <span class="hljs-attribute">line-height</span>: <span class="hljs-number">36px</span>; <span class="hljs-attribute">margin-top</span>: <span class="hljs-number">5px</span>; <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">3px</span>;}<span class="hljs-selector-class">.e-secret</span> <span class="hljs-selector-tag">input</span><span class="hljs-selector-class">.euc-y-s</span><span class="hljs-selector-attr">[type="submit"]</span> { <span class="hljs-attribute">float</span>: right; <span class="hljs-attribute">margin-top</span>: -<span class="hljs-number">47px</span>; <span class="hljs-attribute">width</span>: <span class="hljs-number">30%</span>; <span class="hljs-attribute">margin-right</span>: <span class="hljs-number">1px</span>; <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">0</span> <span class="hljs-number">3px</span> <span class="hljs-number">3px</span> <span class="hljs-number">0</span>;}<span class="hljs-selector-tag">input</span><span class="hljs-selector-class">.euc-y-s</span><span class="hljs-selector-attr">[type="submit"]</span>{<span class="hljs-attribute">background-color</span>:<span class="hljs-number">#3498db</span>;<span class="hljs-attribute">color</span>:<span class="hljs-number">#fff</span>;<span class="hljs-attribute">font-size</span>:<span class="hljs-number">21px</span>;<span class="hljs-attribute">box-shadow</span>:none;<span class="hljs-attribute">-webkit-transition</span>: .<span class="hljs-number">4s</span>;<span class="hljs-attribute">-moz-transition</span>: .<span class="hljs-number">4s</span>;<span class="hljs-attribute">-o-transition</span>: .<span class="hljs-number">4s</span>;<span class="hljs-attribute">transition</span>:.<span class="hljs-number">4s</span>;<span class="hljs-attribute">-webkit-backface-visibility</span>:hidden;<span class="hljs-attribute">position</span>:relative;<span class="hljs-attribute">cursor</span>:pointer;<span class="hljs-attribute">padding</span>: <span class="hljs-number">13px</span> <span class="hljs-number">20px</span>;<span class="hljs-attribute">text-align</span>: center;<span class="hljs-attribute">border-radius</span>: <span class="hljs-number">50px</span>;<span class="hljs-attribute">-webkit-box-shadow</span>: none; <span class="hljs-attribute">-moz-box-shadow</span>: none; <span class="hljs-attribute">box-shadow</span>: none;<span class="hljs-attribute">border</span>: <span class="hljs-number">0</span>;<span class="hljs-attribute">height</span>: auto;<span class="hljs-attribute">outline</span>: medium;<span class="hljs-attribute">line-height</span>: <span class="hljs-number">20px</span>;<span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;}<span class="hljs-selector-tag">input</span><span class="hljs-selector-class">.euc-y-s</span><span class="hljs-selector-attr">[type="submit"]</span><span class="hljs-selector-pseudo">:hover</span>{<span class="hljs-attribute">background-color</span>:<span class="hljs-number">#5dade2</span>;}<span class="hljs-selector-tag">input</span><span class="hljs-selector-class">.euc-y-i</span><span class="hljs-selector-attr">[type="text"]</span>,<span class="hljs-selector-tag">input</span><span class="hljs-selector-class">.euc-y-i</span><span class="hljs-selector-attr">[type="password"]</span>{<span class="hljs-attribute">border</span>:<span class="hljs-number">1px</span> solid <span class="hljs-number">#F2EFEF</span>;<span class="hljs-attribute">color</span>:<span class="hljs-number">#777</span>;<span class="hljs-attribute">display</span>:block;<span class="hljs-attribute">background</span>: <span class="hljs-number">#FCFCFC</span>;<span class="hljs-attribute">font-size</span>:<span class="hljs-number">18px</span>;<span class="hljs-attribute">transition</span>:all .<span class="hljs-number">5s</span> ease <span class="hljs-number">0</span>;<span class="hljs-attribute">outline</span>:<span class="hljs-number">0</span>;<span class="hljs-attribute">box-sizing</span>:border-box;<span class="hljs-attribute">-webkit-border-radius</span>:<span class="hljs-number">25px</span>;<span class="hljs-attribute">-moz-border-radius</span>:<span class="hljs-number">25px</span>;<span class="hljs-attribute">border-radius</span>:<span class="hljs-number">25px</span>;<span class="hljs-attribute">padding</span>:<span class="hljs-number">5px</span> <span class="hljs-number">16px</span>; <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;<span class="hljs-attribute">height</span>: auto;<span class="hljs-attribute">line-height</span>: <span class="hljs-number">30px</span>;}<span class="hljs-selector-tag">input</span><span class="hljs-selector-class">.euc-y-i</span><span class="hljs-selector-attr">[type="text"]</span><span class="hljs-selector-pseudo">:hover</span>,<span class="hljs-selector-tag">input</span><span class="hljs-selector-class">.euc-y-i</span><span class="hljs-selector-attr">[type="password"]</span><span class="hljs-selector-pseudo">:hover</span>{<span class="hljs-attribute">border</span>:<span class="hljs-number">1px</span> solid <span class="hljs-number">#56b4ef</span>;<span class="hljs-attribute">box-shadow</span>:<span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">4px</span> <span class="hljs-number">#56b4ef</span>;}<span class="hljs-comment">/*e-secret*/</span>
<span class="hljs-selector-class">.e-secret</span> {
<span class="hljs-attribute">margin</span>: <span class="hljs-number">20px</span> <span class="hljs-number">0</span>;
<span class="hljs-attribute">padding</span>: <span class="hljs-number">20px</span>;
<span class="hljs-attribute">background</span>: <span class="hljs-number">#f8f8f8</span>;
}
<span class="hljs-selector-class">.e-secret</span> <span class="hljs-selector-tag">input</span><span class="hljs-selector-class">.euc-y-i</span><span class="hljs-selector-attr">[type="password"]</span> {
<span class="hljs-attribute">float</span>: left;
<span class="hljs-attribute">background</span>: <span class="hljs-number">#fff</span>;
<span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
<span class="hljs-attribute">line-height</span>: <span class="hljs-number">36px</span>;
<span class="hljs-attribute">margin-top</span>: <span class="hljs-number">5px</span>;
<span class="hljs-attribute">border-radius</span>: <span class="hljs-number">3px</span>;
}
<span class="hljs-selector-class">.e-secret</span> <span class="hljs-selector-tag">input</span><span class="hljs-selector-class">.euc-y-s</span><span class="hljs-selector-attr">[type="submit"]</span> {
<span class="hljs-attribute">float</span>: right;
<span class="hljs-attribute">margin-top</span>: -<span class="hljs-number">47px</span>;
<span class="hljs-attribute">width</span>: <span class="hljs-number">30%</span>;
<span class="hljs-attribute">margin-right</span>: <span class="hljs-number">1px</span>;
<span class="hljs-attribute">border-radius</span>: <span class="hljs-number">0</span> <span class="hljs-number">3px</span> <span class="hljs-number">3px</span> <span class="hljs-number">0</span>;
}
<span class="hljs-selector-tag">input</span><span class="hljs-selector-class">.euc-y-s</span><span class="hljs-selector-attr">[type="submit"]</span>{<span class="hljs-attribute">background-color</span>:<span class="hljs-number">#3498db</span>;<span class="hljs-attribute">color</span>:<span class="hljs-number">#fff</span>;<span class="hljs-attribute">font-size</span>:<span class="hljs-number">21px</span>;<span class="hljs-attribute">box-shadow</span>:none;<span class="hljs-attribute">-webkit-transition</span>: .<span class="hljs-number">4s</span>;<span class="hljs-attribute">-moz-transition</span>: .<span class="hljs-number">4s</span>;<span class="hljs-attribute">-o-transition</span>: .<span class="hljs-number">4s</span>;<span class="hljs-attribute">transition</span>:.<span class="hljs-number">4s</span>;<span class="hljs-attribute">-webkit-backface-visibility</span>:hidden;<span class="hljs-attribute">position</span>:relative;<span class="hljs-attribute">cursor</span>:pointer;<span class="hljs-attribute">padding</span>: <span class="hljs-number">13px</span> <span class="hljs-number">20px</span>;<span class="hljs-attribute">text-align</span>: center;<span class="hljs-attribute">border-radius</span>: <span class="hljs-number">50px</span>;<span class="hljs-attribute">-webkit-box-shadow</span>: none; <span class="hljs-attribute">-moz-box-shadow</span>: none; <span class="hljs-attribute">box-shadow</span>: none;<span class="hljs-attribute">border</span>: <span class="hljs-number">0</span>;<span class="hljs-attribute">height</span>: auto;<span class="hljs-attribute">outline</span>: medium;<span class="hljs-attribute">line-height</span>: <span class="hljs-number">20px</span>;<span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;}
<span class="hljs-selector-tag">input</span><span class="hljs-selector-class">.euc-y-s</span><span class="hljs-selector-attr">[type="submit"]</span><span class="hljs-selector-pseudo">:hover</span>{<span class="hljs-attribute">background-color</span>:<span class="hljs-number">#5dade2</span>;}
<span class="hljs-selector-tag">input</span><span class="hljs-selector-class">.euc-y-i</span><span class="hljs-selector-attr">[type="text"]</span>,<span class="hljs-selector-tag">input</span><span class="hljs-selector-class">.euc-y-i</span><span class="hljs-selector-attr">[type="password"]</span>{<span class="hljs-attribute">border</span>:<span class="hljs-number">1px</span> solid <span class="hljs-number">#F2EFEF</span>;<span class="hljs-attribute">color</span>:<span class="hljs-number">#777</span>;<span class="hljs-attribute">display</span>:block;<span class="hljs-attribute">background</span>: <span class="hljs-number">#FCFCFC</span>;<span class="hljs-attribute">font-size</span>:<span class="hljs-number">18px</span>;<span class="hljs-attribute">transition</span>:all .<span class="hljs-number">5s</span> ease <span class="hljs-number">0</span>;<span class="hljs-attribute">outline</span>:<span class="hljs-number">0</span>;<span class="hljs-attribute">box-sizing</span>:border-box;<span class="hljs-attribute">-webkit-border-radius</span>:<span class="hljs-number">25px</span>;<span class="hljs-attribute">-moz-border-radius</span>:<span class="hljs-number">25px</span>;<span class="hljs-attribute">border-radius</span>:<span class="hljs-number">25px</span>;<span class="hljs-attribute">padding</span>:<span class="hljs-number">5px</span> <span class="hljs-number">16px</span>; <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;<span class="hljs-attribute">height</span>: auto;<span class="hljs-attribute">line-height</span>: <span class="hljs-number">30px</span>;}
<span class="hljs-selector-tag">input</span><span class="hljs-selector-class">.euc-y-i</span><span class="hljs-selector-attr">[type="text"]</span><span class="hljs-selector-pseudo">:hover</span>,<span class="hljs-selector-tag">input</span><span class="hljs-selector-class">.euc-y-i</span><span class="hljs-selector-attr">[type="password"]</span><span class="hljs-selector-pseudo">:hover</span>{<span class="hljs-attribute">border</span>:<span class="hljs-number">1px</span> solid <span class="hljs-number">#56b4ef</span>;<span class="hljs-attribute">box-shadow</span>:<span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">4px</span> <span class="hljs-number">#56b4ef</span>;}
/*e-secret*/
.e-secret {
margin: 20px 0;
padding: 20px;
background: #f8f8f8;
}
.e-secret input.euc-y-i[type="password"] {
float: left;
background: #fff;
width: 100%;
line-height: 36px;
margin-top: 5px;
border-radius: 3px;
}
.e-secret input.euc-y-s[type="submit"] {
float: right;
margin-top: -47px;
width: 30%;
margin-right: 1px;
border-radius: 0 3px 3px 0;
}
input.euc-y-s[type="submit"]{background-color:#3498db;color:#fff;font-size:21px;box-shadow:none;-webkit-transition: .4s;-moz-transition: .4s;-o-transition: .4s;transition:.4s;-webkit-backface-visibility:hidden;position:relative;cursor:pointer;padding: 13px 20px;text-align: center;border-radius: 50px;-webkit-box-shadow: none; -moz-box-shadow: none; box-shadow: none;border: 0;height: auto;outline: medium;line-height: 20px;margin: 0;}
input.euc-y-s[type="submit"]:hover{background-color:#5dade2;}
input.euc-y-i[type="text"],input.euc-y-i[type="password"]{border:1px solid #F2EFEF;color:#777;display:block;background: #FCFCFC;font-size:18px;transition:all .5s ease 0;outline:0;box-sizing:border-box;-webkit-border-radius:25px;-moz-border-radius:25px;border-radius:25px;padding:5px 16px; margin: 0;height: auto;line-height: 30px;}
input.euc-y-i[type="text"]:hover,input.euc-y-i[type="password"]:hover{border:1px solid #56b4ef;box-shadow:0 0 4px #56b4ef;}
如何实现?在编辑文章是使用短码包围要隐藏的内容,如下即可。
【secret key=<span class="hljs-string">"密码"</span>】加密内容【/secret】【secret key=<span class="hljs-string">"密码"</span>】加密内容【/secret】
【secret key="密码"】加密内容【/secret】
把上面中文括号改为英文括号【】→[]
本站使用输入密码可见示例
测试密码:123456
4、回复后可见
首先在主题functions.php文件中添加下面代码。
<span class="hljs-comment">//部分内评论可见</span><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">reply_read</span><span class="hljs-params">($atts, $content=null)</span> </span>{extract(shortcode_atts(<span class="hljs-keyword">array</span>(<span class="hljs-string">"notice"</span> => <span class="hljs-string">'<span style="color: #1abec7;"> *** 隐藏内容,回复后可见!*** </span>'</span>), $atts));$email = <span class="hljs-keyword">null</span>;$user_ID = (int) wp_get_current_user()->ID;<span class="hljs-keyword">if</span> ($user_ID > <span class="hljs-number">0</span>) { $email = get_userdata($user_ID)->user_email;<span class="hljs-keyword">if</span> ( current_user_can(<span class="hljs-string">'level_10'</span>) ) { <span class="hljs-keyword">return</span> <span class="hljs-string">'<span style="color: #08a007;">'</span>.do_shortcode( $content ).<span class="hljs-string">'</span>'</span>;}<span class="hljs-keyword">if</span> ($email == $admin_email) { <span class="hljs-keyword">return</span> do_shortcode($content);}} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-keyword">isset</span>($_COOKIE[<span class="hljs-string">'comment_author_email_'</span> . COOKIEHASH])) { $email = str_replace(<span class="hljs-string">'%40'</span>, <span class="hljs-string">'@'</span>, $_COOKIE[<span class="hljs-string">'comment_author_email_'</span> . COOKIEHASH]);} <span class="hljs-keyword">else</span> { <span class="hljs-keyword">return</span> $notice;}<span class="hljs-keyword">if</span> (<span class="hljs-keyword">empty</span>($email)) { <span class="hljs-keyword">return</span> $notice;}<span class="hljs-keyword">global</span> $wpdb;$post_id = get_the_ID();$query = <span class="hljs-string">"SELECT `comment_ID` FROM {$wpdb->comments} WHERE `comment_post_ID`={$post_id} and `comment_approved`='1' and `comment_author_email`='{$email}' LIMIT 1"</span>;<span class="hljs-keyword">if</span> ($wpdb->get_results($query)) { <span class="hljs-keyword">return</span> do_shortcode(<span class="hljs-string">'<span style="color: #1abec7;">'</span>.do_shortcode( $content ).<span class="hljs-string">'</span>'</span>);} <span class="hljs-keyword">else</span> { <span class="hljs-keyword">return</span> $notice;}}add_shortcode(<span class="hljs-string">'hide'</span>, <span class="hljs-string">'reply_read'</span>);<span class="hljs-comment">//部分内评论可见</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">reply_read</span><span class="hljs-params">($atts, $content=null)</span> </span>{
extract(shortcode_atts(<span class="hljs-keyword">array</span>(<span class="hljs-string">"notice"</span> => <span class="hljs-string">'<span style="color: #1abec7;"> *** 隐藏内容,回复后可见!*** </span>'</span>), $atts));
$email = <span class="hljs-keyword">null</span>;
$user_ID = (int) wp_get_current_user()->ID;
<span class="hljs-keyword">if</span> ($user_ID > <span class="hljs-number">0</span>) {
$email = get_userdata($user_ID)->user_email;
<span class="hljs-keyword">if</span> ( current_user_can(<span class="hljs-string">'level_10'</span>) ) {
<span class="hljs-keyword">return</span> <span class="hljs-string">'<span style="color: #08a007;">'</span>.do_shortcode( $content ).<span class="hljs-string">'</span>'</span>;
}
<span class="hljs-keyword">if</span> ($email == $admin_email) {
<span class="hljs-keyword">return</span> do_shortcode($content);
}
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-keyword">isset</span>($_COOKIE[<span class="hljs-string">'comment_author_email_'</span> . COOKIEHASH])) {
$email = str_replace(<span class="hljs-string">'%40'</span>, <span class="hljs-string">'@'</span>, $_COOKIE[<span class="hljs-string">'comment_author_email_'</span> . COOKIEHASH]);
} <span class="hljs-keyword">else</span> {
<span class="hljs-keyword">return</span> $notice;
}
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">empty</span>($email)) {
<span class="hljs-keyword">return</span> $notice;
}
<span class="hljs-keyword">global</span> $wpdb;
$post_id = get_the_ID();
$query = <span class="hljs-string">"SELECT `comment_ID` FROM {$wpdb->comments} WHERE `comment_post_ID`={$post_id} and `comment_approved`='1' and `comment_author_email`='{$email}' LIMIT 1"</span>;
<span class="hljs-keyword">if</span> ($wpdb->get_results($query)) {
<span class="hljs-keyword">return</span> do_shortcode(<span class="hljs-string">'<span style="color: #1abec7;">'</span>.do_shortcode( $content ).<span class="hljs-string">'</span>'</span>);
} <span class="hljs-keyword">else</span> {
<span class="hljs-keyword">return</span> $notice;
}
}
add_shortcode(<span class="hljs-string">'hide'</span>, <span class="hljs-string">'reply_read'</span>);
//部分内评论可见
function reply_read($atts, $content=null) {
extract(shortcode_atts(array("notice" => '<span style="color: #1abec7;"> *** 隐藏内容,回复后可见!*** </span>'), $atts));
$email = null;
$user_ID = (int) wp_get_current_user()->ID;
if ($user_ID > 0) {
$email = get_userdata($user_ID)->user_email;
if ( current_user_can('level_10') ) {
return '<span style="color: #08a007;">'.do_shortcode( $content ).'</span>';
}
if ($email == $admin_email) {
return do_shortcode($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('<span style="color: #1abec7;">'.do_shortcode( $content ).'</span>');
} else {
return $notice;
}
}
add_shortcode('hide', 'reply_read');
如何实现?在编辑文章是使用短码包围要隐藏的内容,下面列举的是两种方式,我们任选一种即可。
【hide】我是被隐藏的内容,样式一(默认样式)【/hide】【hide】 notice=<span class="hljs-string">"自定义信息"</span>]我是被隐藏的内容,样式二(自定义回复信息)【/hide】【hide】我是被隐藏的内容,样式一(默认样式)【/hide】
【hide】 notice=<span class="hljs-string">"自定义信息"</span>]我是被隐藏的内容,样式二(自定义回复信息)【/hide】
【hide】我是被隐藏的内容,样式一(默认样式)【/hide】
【hide】 notice="自定义信息"]我是被隐藏的内容,样式二(自定义回复信息)【/hide】
把上面中文括号改为英文括号【】→[]
请登录后发表评论
注册