我需要根据特定的用户角色显示管理菜单的现有子菜单项。角色是general-travel-editor
。
实际的菜单布局就像这样
旅行设施
- 常规设置
- 高级设置
以上用户角色应该只访问"一般设置"子菜单。所以我想将此类用户的菜单显示为:
旅行设施
- 常规设置
我尝试了add_menu_page( 'Travel Settings', 'Travel Settings', 'general-travel-editor', 'general-travel-editor');
,但我没有选择添加此页面的网址。
我正在使用VIP插件。 我需要在今晚之前提供这个但是卡住了!
有人请告诉我如何做到这一点。
更新
在functions.php中定义的用户角色和功能
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');
旅行设置:
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' );
travel-option
是自定义帖子类型,因此我可以在caps
数组中使用它。使用上述用户角色登录时,我可以看到CPT菜单很好,但是如何添加此设置菜单?我为张贴这么长的代码而道歉!
答案 0 :(得分:0)
add_submenu_page函数 - 您应该用于“高级设置”,其定义如下:
add_submenu_page(string $ parent_slug,string $ page_title,string $ menu_title, string $ capability ,string $ menu_slug,callable $ function ='');
如您所见,您可以定义某个$capability
来显示您的菜单或子菜单页。
您可以使用仅适用于manage_options
等管理员的功能,或者您甚至可以定义这样的特殊功能:
$role = get_role( 'administrator' );
$role->add_cap( 'edit_advanced_travel_settings' );
https://codex.wordpress.org/Function_Reference/add_cap
如果您正确执行此操作,则只有您已授予此功能的用户才能看到高级页面。
注意:在自定义管理页面回调中 - 出于安全原因 - 您还应检查当前用户是否具有所需的功能。
if(current_user_can('edit_advanced_travel_settings')) {
// display page
} else {
echo "Sorry advanced travel settings are not available to you.";
}