【问题标题】:create admin menu and page for a custom user role为自定义用户角色创建管理菜单和页面
【发布时间】:2015-12-02 11:23:40
【问题描述】:

我正在制作具有自定义用户角色的自定义插件:

add_role('lln_assessor', 'LLN Assessor', array(

'read' => true, // true allows this capability
'edit_posts' => true, // Allows user to edit their own posts
'edit_pages' => true, // Allows user to edit pages
'edit_others_posts' => true, // Allows user to edit others posts not just their own
'create_posts' => true, // Allows user to create new posts
'manage_categories' => true, // Allows user to manage post categories
'publish_posts' => true, // Allows the user to publish, otherwise posts stays in draft mode
));

这个角色将在我激活我的自定义插件时创建。

现在,在 wp-admin 或其登录时的仪表板中,如何创建自己的菜单和页面?

【问题讨论】:

    标签: php wordpress user-roles


    【解决方案1】:

    这里的 custom_cap 是添加到新角色的自定义功能,并且在添加新的管理菜单时添加了此功能。

      add_role('lln_assessor', 'LLN Assessor', array(
    
        'read' => true, // true allows this capability
        'edit_posts' => true, // Allows user to edit their own posts
        'edit_pages' => true, // Allows user to edit pages
        'edit_others_posts' => true, // Allows user to edit others posts not just their own
        'create_posts' => true, // Allows user to create new posts
        'manage_categories' => true, // Allows user to manage post categories
        'publish_posts' => true, // Allows the user to publish, otherwise posts stays in draft mode
        'custom_cap'=>true
        ));
    
            add_menu_page( 'Custom Menu', 'Custom Menu', 'custom_cap', 'menu-slug', 'menu_function', plugins_url( 'icon.png' ), '1.0' );
    
        function menu_function(){
    
          // do code for menu here
    
        }
    
       }
    

    【讨论】:

    • 感谢 Gaurav 的回复,但我收到此错误:Fatal error: Call to undefined function wp_get_current_user() in C:\xampp\htdocs\tide\wp-includes\capabilities.php on line 1441
    • 你在哪里使用这个 wp_get_current_user() 插件或主题。如果您使用任何钩子,请指定。
    • 现在我得到了You do not have sufficient permissions to access this page. 错误..这是我当前的代码add_menu_page('Axcelerate LLN','LLN Datas','assess_lln', 'Axcelerate_Link_Admin_lln_data','axcelerate_link_admin_lln_data_fn','','1.0' );,其中assess_lln 是我的custom_cap 我认为错误在于自定义功能.. 请帮助跨度>
    • 你应该试试code $currentuser= wp_get_current_user(); $role=get_user_role($currentuser->ID); if($role=='lln_assessor'){ add_menu_page('Axcelerate LLN','LLN 数据','manage_categories', 'Axcelerate_Link_Admin_lln_data','axcelerate_link_admin_lln_data_fn','','1.0' );函数 get_user_role($uid) { 全局 $wpdb; $role = $wpdb->get_var("SELECT meta_value FROM {$wpdb->usermeta} where meta_key = 'wp_capabilities' AND user_id = {$uid}"); if(!$role) 返回“非用户”; $rarr = 反序列化($role); $roles = is_array($rarr) ? array_keys($rarr) : array('非用户');返回$角色[0]; } } code
    • manage_categories 不会工作.. 现在我尝试manage_options 然后它工作.. 然后我添加一些 CSS 来隐藏不需要的管理菜单页面并显示我想要显示的内容
    猜你喜欢
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-07
    • 2014-02-11
    相关资源
    最近更新 更多