使用添加角色挂钩仅显示自定义用户角色用户的自定义插件菜单

时间:2020-10-19 14:21:10

标签: wordpress plugins user-roles

我已经在 functions.php 中为新用户创建了自定义用户角色,如以下代码所示:

add_action( 'admin_init', 'add_custom_roles' );

function add_custom_roles() {

    $student = add_role( 'student', 'Student', array(
        'read' => false,
        'assignments' => true,
    ));

    if( null !== $student ) {
        $role->add_cap( 'assignments', true);
   }

} 

只允许访问student角色以查看我的自定义插件页面:assignments,但它不起作用。自定义插件是通过add_menu_page钩子创建的。

1 个答案:

答案 0 :(得分:0)

您已接近目标,要帮助自己,您可以安装此(https://it.wordpress.org/plugins/members/),使功能处理更容易,然后调整设置以获得所需的菜单可见性。

您应该“克隆”进入后端所需的最低WP用户角色(如果我记得不错,应该是贡献者),然后添加访问插件页面的自定义权限