douhua1890 2017-05-11 11:06
浏览 91
已采纳

WordPress - 自定义用户角色 - 授予访客访问edit.php而无需插入/更新/删除

I am working on a site where I want to show customers a demo-version of a product. To do this they can login with a guest-account, I have created this role as follows

add_role('guest', __('Guest'), array('read'=> true));

When I login as a guest I get redirected to and get to see the overview-page /admin.php?page=my_overview, so this does work, but when the guest clicks on any item in this overview I get a message saying: You don't have permission to access this page.

These items refer to pages like: /profile.php /edit.php /users.php /admin.php Since using these pages is a basic part of my products functionality, I want to grant the guest account access to these pages, but don't let them insert/update/delete any data which is shown on those pages.

I have tried adding multiple different privileges i.e. edit_posts, edit_private_posts, edit_private_pages, read_private_posts, read_private_pages but I can't get it to work right.

I would like to achieve this guest account with specific permissions without the use of any plugin. Basically I want to create a user that can do the same as an Author, but without any INSERTs/UPDATEs/DELETEs on my demo-site to the database

I don't know if it is even possible, but any help would be appreciated.

  • 写回答

1条回答 默认 最新

  • doudong2149 2017-05-11 11:52
    关注

    I have figured it out, I was only creating a role, but I didn't add any capabilities to it. The code I was missing will be like and its working now:

    function add_capability() {
        // gets the author role
        $role = get_role( 'guest' );
        // This only works, because it accesses the class instance.
        $role->add_cap( 'edit_others_posts' ); 
        $role->add_cap( 'read_post' ); 
        $role->add_cap( 'edit_posts' ); 
    }
    add_action( 'admin_init', 'add_capability');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?