本篇文章主要集合了网络上对WordPress文章中部分隐藏内容各种可见方法大全,包括登录可见、评论后可见、登录并评论后才可见、输入密码查看、根据用户权限不同隐藏内容实现代码等各种实现方法,好了不多说了,直接开始。
将以上代码加入主题functions.php
文件中,然后在文章中使用短代码调用即可。
登录可见
// 登录可见
function jsk_login_shortcode( $atts, $content ) {
if( !is_null( $content ) && !is_user_logged_in() ) $content = '<div class="bg-lr2v">' . __('<span><i class="fa fa-lock"></i> 特别提醒:</span>因内容只分享给有品位的人,待您 <a href="javascript:;" class="signin-loader">登录</a> 后即可查看,请谅解!','iesay') . '</div>';
return $content;
}
add_shortcode( 'jsk_login', 'jsk_login_shortcode' );
评论后可见
// 隐藏部分内容 评论后可见
function jsk_reply_shortcode($atts, $content=null) {
extract(shortcode_atts(array("notice" => '<p class="bg-lr2v"><span><i class="fa fa-lock"></i> 特别提醒:</span>因内容只分享给有品位的人,待您</span> <a href="#comments" 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 = "xxx@xxx.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('jsk_reply', 'jsk_reply_shortcode');
登录并评论后才可见
// 登录并评论后才可见
function jsk_reply_login_shortcode( $atts, $content ){
if( !is_null( $content ) ) :
if(!is_user_logged_in()){
$content = '<div class="bg-lr2v">' . __('<span><i class="fa fa-lock"></i> 特别提醒:</span>因内容只分享给有品位的人,此处内容需要 <a href="javascript:;" class="signin-loader">登录</a> 并发表评论后刷新即可查看,请谅解!','iesay') . '</div>';
}else{
global $post;
$user_id = get_current_user_ID();
if( $user_id != $post->post_author && !user_can($user_id,'edit_others_posts') ){
$comments = get_comments( array('status' => 'approve', 'user_id' => get_current_user_ID(), 'post_id' => $post->ID, 'count' => true ) );
if(!$comments) {
$content = '<div class="bg-lr2v">' . __('<span><i class="fa fa-lock"></i> 特别提醒:</span>因内容只分享给有品位的人,此处内容需要登录并 <a href="#comments">发表评论</a> 后刷新即可查看,请谅解!' ,'iesay'). '</div>';
}
}
}
endif;
return $content;
}
add_shortcode( 'jsk_tinr2v', 'jsk_reply_login_shortcode' );
输入密码查看
// 加密内容
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 $content;
}
else{
return '<div class="bg-lr2v">
<form class="e-secret" action="'.get_permalink().'" method="post" name="e-secret"><span><i class="fa fa-lock"></i> 特别提醒:</span>因内容只分享给有品位的人,此处输入密码才可查看:<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></div>';
}
}
add_shortcode('jsk_secret','e_secret');
根据用户权限不同隐藏内容实现代码
add_shortcode( 'jsk_vip', 'users_read_shortcode' );//注册短代码
function users_read_shortcode( $atts, $content = null ) {
if ( is_user_logged_in() && !empty( $content ) && current_user_can( 'read' ) ) {
return $content;
}
if ( !is_user_logged_in()){
return '未登录输出';
}
if ( is_user_logged_in() && !current_user_can( 'read' )){
return '登陆但没有阅读权限输出';
}
}
上面的代码中,我们还使用了用户权限的判断函数current_user_can
,该函数可以读取当前用户是否拥有某项权利。关于用户权限,不清楚请自行百度学习。
© 版权声明
这些信息可能会帮助到你: 下载帮助 | 免责说明 | 进站必看 | 广告投放
版权声明:本文采用知识共享 署名4.0国际许可协议 BY-NC-SA 4.0 进行授权文章名称:《WordPress文章部分内容隐藏可见方法大全》
文章链接:https://www.jsksky.com/some-hidden-content-of-wordpress-article-is-visible.html
免责声明:根据《计算机软件保护条例》第十七条规定“为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬。”您需知晓本站所有内容资源均来源于网络,仅供用户交流学习与研究使用,版权归属原版权方所有,版权争议与本站无关,用户本人下载后不能用作商业或非法用途,需在24个小时之内从您的电脑中彻底删除上述内容,否则后果均由用户承担责任;如果您访问和下载此文件,表示您同意只将此文件用于参考、学习而非其他用途,否则一切后果请您自行承担,如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。
本站为个人博客非盈利性站点,所有软件信息均来自网络,所有资源仅供学习参考研究目的,并不贩卖软件,不存在任何商业目的及用途,部分软件收费或网站会员捐赠是您喜欢本站而产生的赞助支持行为,仅为维持服务器的开支与维护,全凭自愿无任何强求。
THE END
请登录后发表评论
注册
社交帐号登录