WordPress admin: How can I display existing admin sub-menu based on user's role
我需要根据特定用户角色显示管理菜单的现有子菜单项。角色是
实际的菜单布局是这样的
Travel Settings
- General settings
- Advanced Settings
上述用户角色应该只能访问"常规设置"子菜单。所以我想为这种类型的用户显示菜单:
Travel Settings
- General settings
我尝试了
我正在使用 VIP 插件。
我需要在今晚之前交付,但卡住了!
请有人告诉我如何做到这一点。
更新
functions.php中定义的用户角色和能力
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | function general_travel_user_capabilities() { $travel_option_caps = array( 'publish_travel-options' => true, 'edit_travel-options' => true, 'edit_others_travel-options' => true, 'edit_travel-option' => true, 'read_travel-option' => true, 'delete_travel-options' => true, 'delete_travel-option' => true, 'read' => true ); wpcom_vip_duplicate_role('subscriber', 'travel-option-editor', 'Travel Option Editor', $travel_option_caps); $travel_option_caps = array( 'publish_travel-options', 'edit_travel-options', 'edit_others_travel-options', 'delete_travel-options', 'delete_others_travel-options', 'read_private_travel-options', 'edit_travel-option', 'delete_travel-option', 'read_travel-option' ); wpcom_vip_add_role_caps('editor', $travel_option_caps); wpcom_vip_add_role_caps('administrator', $travel_option_caps); } add_action('init', 'general_travel_user_capabilities'); |
旅行设置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | function travel_setting_pages_init() { $travel_settings = travel_create_settings_page( 'travel_settings_page', 'Global Travel Settings', array( 'parent' => false, 'title' => 'Travel Settings', 'position' => '59.4815152342', ), array( 'travel_theme_options' => array( 'title' => '', 'description' => 'Settings that affect all pages of the site', 'fields' => array( 'mytravel_root_url' => array( 'type' => 'url', 'label' => 'MyTravel Root URL', 'description' => 'The base url for all MyTravel App links', 'default' => 'http://myurl.com', ), 'travel_phone' => array( 'type' => 'text', 'label' => 'Support Phone Number U.S.', 'description' => 'In top left of all pages', 'default' => '800-610-6500', ), 'travel_phone_canada' => array( 'type' => 'text', 'label' => 'Support Phone Number Canada', 'description' => 'In top left of all pages', 'default' => '877-330-3321', ), 'travel_weather_text' => array( 'type' => 'text', 'label' => 'Alert Text', 'description' => 'Text for the alert on the home page. Required for the alert to be displayed.', 'default' => '', ), 'travel_weather_link' => array( 'type' => 'url', 'label' => 'Alert Link', 'description' => 'Link for the alert on the home page. Required for the alert to be displayed.', 'default' => '', ), 'travel_weather_start_date' => array( 'type' => 'date', 'label' => 'Alert Start Date', 'description' => 'Start date to display the alert on the home page. Optional.', 'default' => '', ), 'travel_weather_end_date' => array( 'type' => 'date', 'label' => 'Alert end Date', 'description' => 'End date to display the alert on the home page. Optional.', 'default' => '', ) ) ) ) ); // Home Page Settings Sub-Page $slide_color_options = array( 'orange-grunge' => 'Orange Grunge Pattern', 'orange-img1' => 'Orange Image: Truck Engine with Person', 'orange-img2' => 'Orange Image: Truck Top with Person', 'orange-img3' => 'Orange Image: Forklift', 'orange-trailers' => 'Orange Image: Truck Trailers', 'black-trails' => 'Black Image: Light Trails', 'dark-grey' => 'Grey Image: Hand', 'white' => 'White Image: Trucks in Dock', 'orange' => 'Orange (To be removed)', 'blue' => 'Blue (To be removed)', 'black' => 'Black (To be removed)' ); $travel_home_settings = travel_create_settings_page( 'travel_home_settings_page', 'Home Page Settings', array( 'parent' => 'travel_settings_page', 'title' => 'Home Page Settings', ), array( 'travel_home_slideshow_settings' => array( 'title' => 'Home Slideshow Settings', 'description' => 'If image is not set, slide will not be shown.', 'fields' => array( // Slide 1 'slide_color_1' => array( 'type' => 'radio', 'label' => 'Slide 1 Background Color', 'default' => 'orange', 'options' => $slide_color_options ), 'slide_image_1' => array( 'type' => 'media', 'label' => 'Slide 1 Image', 'description' => 'Transparent PNG @ 911x375. See travel-home-marquee-template.psd', ), 'slide_heading_1' => array( 'type' => 'textarea', 'label' => 'Slide 1 Heading', ), 'slide_sub_heading_1' => array( 'type' => 'wysiwyg', 'label' => 'Slide 1 Sub-Heading', ), ) ), 'travel_home_mid_cta_settings' => array( 'title' => 'Middle CTA Settings', 'fields' => array( 'mid_cta_heading_1_1' => array( 'type' => 'text', 'label' => 'Middle CTA 1 - First Line', ), ) ), 'travel_home_lower_cta_settings' => array( // first block 'title' => 'Lower CTA Settings', 'fields' => array( 'lower_cta_image_1' => array( 'type' => 'media', 'label' => 'Lower CTA 1 - Header Image', 'description' => 'Transparent PNG @ 193x71. If set, overrides main heading text.', ), ) ) ) ); } add_action( 'init', 'travel_setting_pages_init' ); |
我会在这里为面临相同情况的任何人留下另一个答案。
对我来说,我正在创建一个插件,并希望给它一个不同于(管理员)的角色
我搜索了已经存在的 wp 角色并选择了"作者"角色。此链接中提到了角色
这是我添加菜单的方法 =>
因此,您只需将 \\'upload_files\\' 替换为与所选角色匹配的任何其他功能。
add_submenu_page 函数 - 您应该用于"高级设置"的定义如下:
add_submenu_page( string $parent_slug, string $page_title, string
$menu_title, string $capability, string $menu_slug, callable $function
= '' );
如您所见,您可以定义一个特定的
你可以使用一个只有管理员可以使用的能力,比如
1 2 | $role = get_role( 'administrator' ); $role->add_cap( 'edit_advanced_travel_settings' ); |
https://codex.wordpress.org/Function_Reference/add_cap
如果您操作正确,高级页面应该只对您授予此功能的用户可见。
注意:在自定义管理页面回调中 - 出于安全原因 - 您还应该检查当前用户是否具有所需的功能。
1 2 3 4 5 | if(current_user_can('edit_advanced_travel_settings')) { // display page } else { echo"Sorry advanced travel settings are not available to you."; } |