dtkyayvldeaqhl7151 2012-09-21 17:06
浏览 98
已采纳

根据角色或功能限制对Wordpress页面的访问

How can I restrict access to a specific Wordpress page based on the user's role or capability? I am aware that there are various plugins that can achieve this and I found some very aged solutions while googling.

I believe it is about time to have an uptodate solution that works.

  • 写回答

1条回答 默认 最新

  • duanshang9426 2012-09-21 18:08
    关注

    As I tried to explain in the comments following my answer to a recent question of yours, and as referenced in the Dev Resources section at the bottom of your linked docs, current_user_can is (as far as I am aware) the most flexible and comprehensive way to test user capabilities given the replacement (2.0) and deprecation (3.0) of User Levels.

    Whether you are permitting or restricting access to view a page or make specific changes, it makes sense to tie in with the capabilities already defined by Wordpress as associated with Roles. This becomes especially true when you use any sort of role scoping, capability editing, or super admin plugins or functions. It is also unhindered by applications in individual or multi-site installations, and covers the entire range of possible users.

    When you test if the current user can do this or that, these capabilities are far less likely to change than which capabilities are associated with certain Roles or User Levels.

    Everything you need to know about current_user_can is fairly well explained in the docs, and it generally follows this logic:

    if ( current_user_can('do_something_pertaining_to_the_below') ) {
        echo 'You are a user who can do the above thing, so do the below thing';
        /* the thing */
    }
    

    Conversely:

    if ( !current_user_can('some_fancy_capability') ) {
        echo 'No can do';
        return;
    }
    else { /* the similarly fancy, restricted thing */ }
    

    All that said, if anyone has heard of a more progressive and intelligent way of doing it, I'm all ears!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c