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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀