WordPress如何判断登录用户的角色
做WordPress主题或插件开发时,常需要判定用户角色是否具备对应权限,我们可以使用 current_user_can() 判断。
current_user_can() 可根据不同角色拥有的权限来判断用户角色,相关用户权限可以在Roles and Capabilities中找到。
1、用户是否为订阅者(Subscriber)
if( current_user_can( 'read' ) && !current_user_can( 'edit_posts' ) ) {
echo 'The current user is a subscriber';
}
2、用户是否为投稿者(Contributor)
if( current_user_can( 'edit_posts' ) && !current_user_can( 'publish_posts' ) ) {
echo 'The current user is a contributor';
}
3、用户是否为管理员(Administrator)
if( current_user_can( 'manage_options' ) ) {
echo 'The current user is a administrator';
}
4、用户是否为作者(Author)
if( current_user_can( 'publish_posts' ) && !current_user_can( 'publish_pages' ) ) {
echo 'The current user is an author';
}
5、用户是否为编辑(Editor)
if( current_user_can( 'publish_pages' ) && !current_user_can( 'manage_options' ) ) {
echo 'The current user is an editor';
}
当然我们还可以使用WordPress全局变量$current_user来判断,具体可以搜索下方法,这里就不做详细的介绍了。
-
WordPress函数:add_plugins_page 在后台侧边栏-插件,添加子菜单使用函数 add_plugins_page 在WordPress侧边栏-插件下添加子菜单
-
WordPress函数:add_users_page 在后台侧边栏-用户,添加子菜单WordPress函数 add_users_page 在后台侧边栏【用户】下添加子菜单
-
WordPress函数:add_options_page 在后台侧边栏-设置,添加子菜单WordPress函数 add_options_page 在后台侧边栏-设置,添加子菜单
-
WordPress分类URL自动添加斜杠有些小伙伴肯定开始疑问了,WordPress分类URL为什么要加斜杠,其实主要还是为了搜索引擎,利于SEO,带斜杠的URL样式如下
-
WordPress( .htaccess )如何禁止SQL查询或禁止垃圾信息请求WordPress如何禁止SQL查询或禁止垃圾信息请求,若我们使用Apache引擎或LAMP环境,我们只需要在根目录的 .htaccess 文件中添加以下代码即可。
-
WordPress函数:wp_generate_uuid4 和 wp_is_uuid 生成和判断UUIDWordPress提供了生成和判断UUID的函数
暂无评论,抢个沙发...