限制特定用户角色的管理页面

时间:2021-04-11 作者:Juraj

我已创建新的用户角色“;广告客户“;并添加了一些功能:

function prefix_add_user_role() {
    add_role(
        \'advertiser\',
        \'Advertiser\',
        array(
            \'switch_themes\' => 0,
            \'edit_themes\' => 0,
            \'activate_plugins\' => 0,
            \'edit_plugins\' => 0,
            \'edit_users\' => 0,
            \'edit_files\' => 0,
            \'manage_options\' => 0,
            \'moderate_comments\' => 0,
            \'manage_categories\' => 0,
            \'manage_links\' => 0,
            \'upload_files\' => 0,
            \'import\' => 0,
            \'unfiltered_html\' => 0,
            \'edit_posts\' => 1,//
            \'edit_others_posts\' => 0,
            \'edit_published_posts\' => 1,//
            \'publish_posts\' => 1,//
            \'edit_pages\' => 0,
            \'read\' => 1,
            \'edit_others_pages\' => 0,
            \'edit_published_pages\' => 0,
            \'publish_pages\' => 0,
            \'delete_pages\' => 0,
            \'delete_others_pages\' => 0,
            \'delete_published_pages\' => 0,
            \'delete_posts\' => 1,
            \'delete_others_posts\' => 0,
            \'delete_published_posts\' => 1,
            \'delete_private_posts\' => 0,
            \'edit_private_posts\' => 0,
            \'read_private_posts\' => 0,
            \'delete_private_pages\' => 0,
            \'edit_private_pages\' => 0,
            \'read_private_pages\' => 0,
            \'delete_users\' => 0,
            \'create_users\' => 0,
            \'unfiltered_upload\' => 0,
            \'edit_dashboard\' => 0,
            \'update_plugins\' => 0,
            \'delete_plugins\' => 0,
            \'install_plugins\' => 0,
            \'update_themes\' => 0,
            \'install_themes\' => 0,
            \'update_core\' => 0,
            \'list_users\' => 0,
            \'remove_users\' => 0,
            \'promote_users\' => 0,
            \'edit_theme_options\' => 0,
            \'delete_themes\' => 0,
            \'export\' => 0,
            \'edit_comment\' => 0,

            \'loco_admin\' => 0,
        )
    );
}
add_action( \'init\', \'prefix_add_user_role\' );
具有广告客户角色的用户根本不应该访问默认的管理页面及其子页面,如帖子(edit.php但不是CPTs页面)、页面、评论(edit comments.php)、联系表单7页面(admin.php?page=wpcf7)。大多数设置为false的功能都是完美的。但可能有些功能工作不正常,或者我做错了什么。广告客户只能访问三种自定义帖子类型、其个人资料和仪表板。

enter image description here

1 个回复
SO网友:Omar Shishani

您可以使用remove_menu_page() 钩子删除菜单项,但如果用户键入URL,页面仍可以访问。Source: WPMayor

要查找要删除的“contact”页面,请从contact admin页面抓取slug并将其粘贴为参数,例如:。remove_menu_page( "[Your admin page\'s slug]" ). 我相信以下这些也是鼻涕虫:

add_action( \'admin_init\', \'my_remove_menu_pages\' );
function my_remove_menu_pages() {

  global $user_ID;

  if ( current_user_can( \'advertiser\' ) ) {
    remove_menu_page(\'tools.php\'); // Tools
    remove_menu_page(\'edit.php\'); // Posts
    remove_menu_page(\'edit-comments.php\'); // Comments 
  }
}

相关推荐