【问题标题】:Wordpress Restrict Access to Admin Area based on RoleWordpress 根据角色限制对管理区域的访问
【发布时间】:2013-10-26 10:26:07
【问题描述】:

试图根据 wordpress 3.6 中的角色限制对管理区域的访问

尝试了以下方法。这可以防止没有管理员访问权限的任何人,但不能防止我的自定义角色“超级用户”。这会一直重定向到主页。

function prevent_admin_access()
{
if ( false !== strpos( strtolower( $_SERVER['REQUEST_URI'] ), '/wp-admin' ) && !current_user_can( 'administrator' ) && !current_user_can( 'Super User' ) )
wp_redirect( home_url() );
}
add_action( 'init', 'prevent_admin_access', 0 );

【问题讨论】:

  • 修复了“超级用户”角色中间需要下划线 - 它不喜欢空格
  • 看看我的回答,为什么你不应该那样做

标签: php wordpress


【解决方案1】:

使用add_role() 添加新角色时,您(或插件)定义了“角色名称”和“角色显示名称”(http://codex.wordpress.org/Function_Reference/add_role)。

current_user_can() 采用名称,而不是显示名称,即“区分大小写,并且应该全部小写”(参见 http://codex.wordpress.org/Function_Reference/current_user_can

在你的情况下,我猜是

... && !current_user_can( 'super_user' ) ...

编辑:

直到现在我才看到您将角色而不是能力传递给current_user_can()。这会起作用(至少在 WP 3.6 中),但不要那样做。

来自文档(上面的链接):

不要将角色名称传递给 current_user_can(),因为这不能保证正常工作(请参阅 #22624)。相反,您不妨试试 AppThemes 整理的check user role function

我建议您使用一些只有管理员和您的超级用户才有的功能,可能是update_core 或类似的东西。

【讨论】:

    猜你喜欢
    • 2012-09-14
    • 2011-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    相关资源
    最近更新 更多