wordpress判断用户是否登陆的函数is_user_logged_in()
is_user_logged_in()函数可以判断当前访问者是否登陆,返回布尔值true或false。通过该函数可以实现为登陆用户与非登陆用户输入不同的内容,也可以限制非登陆用户查询页面内容。
函数用法:
通过if语句使用:
1 2 3 4 5 |
<?php if (is_user_logged_in()){ ... } ?> |
根据当前访问者的登录情况,使用is_user_logged_in() 函数在主题文件中展示不同输出结果。
1 2 3 4 5 6 7 |
<?php if (is_user_logged_in()){ echo "亲爱的会员,您已经登陆!"; }else { echo "欢迎光临,请登陆查看!"; }; ?> |
wordpress current_user_can 判断角色
current_user_can
使用说明:
current_user_can( $capability, $args );
- $capability参数是必须的,是指给予什么样的能力,或者说给他可以做什么;
- $args 参数是非必要的,是额外给予的参数,例如:current_user_can('edit_post', 121),意指给他可以编辑编号为121的文章。
角色判断
if( current_user_can('administrator') ) {
echo '這個文字只有管理員才能看的到';
}
或者
if( current_user_can('level_10') ) {
echo '這個文字只有管理員才能看的到';
}
- 补充说明:
level 数字的判断方式,。官方已经建议不要再使用了,应尽量避免。
-
level_10 ~ level_8:只有 administrator 等级有权限
-
level_7 ~ level_3:只有 administrator、editor 等级有权限
-
level_2:只有 administrator、editor、author 等级有权限
-
level_1:只有 administrator、editor、author、contributor 等级有权限
-
level_0:只有 administrator、editor、author、contributor、subscriber 等级有权限
-
管理员:administrator
-
编辑: editor
-
作者: autor
-
投稿者: contributor
-
订阅者:subscriber
使用举例:
1 判断用户是否为管理员(administrator)
if ( current_user_can ( 'manage_options' ) ) {
echo 'The current user is a administrator' ;
}
2 判断用户是否为编辑(Editor)
if ( current_user_can ( 'publish_pages' ) && ! current_user_can ( 'manage_options' ) ) {
echo 'The current user is an editor' ;
}
3 判断用户是否为作者(Author)
if ( current_user_can ( 'publish_posts' ) && ! current_user_can ( 'publish_pages' ) ) {
echo 'The current user is an author' ;
}
4 判断用户是否为投稿者(Contributor)
if ( current_user_can ( 'edit_posts' ) && ! current_user_can ( 'publish_posts' ) ) {
echo 'The current user is a contributor' ;
}
5 判断用户是否为订阅者(Subscriber)
if ( current_user_can ( 'read' ) && ! current_user_can ( 'edit_posts' ) ) {
echo 'The current user is a subscriber' ;
}