2

我需要根据特定用户角色显示管理菜单的现有子菜单项。角色是general-travel-editor

实际的菜单布局是这样的

旅行设置
- 常规设置
- 高级设置

上述用户角色应该只能访问“常规设置”子菜单。所以我想为这种类型的用户显示菜单:

旅行设置
- 常规设置

我试过add_menu_page( 'Travel Settings', 'Travel Settings', 'general-travel-editor', 'general-travel-editor');了,但我没有添加此页面 URL 的选项。

我正在使用 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 菜单很好地出现,但我如何添加此设置菜单?我很抱歉发布这么长的代码!

4

2 回答 2

1

对于面临相同情况的任何人,我都会在这里留下另一个答案。

对我来说,我正在创建一个插件,并希望给它一个不同于(管理员)的角色

我搜索了已经存在的 wp 角色并选择了“作者”角色。此链接中提到了角色

这是我添加菜单的方法=>

add_menu_page( 'Page Title', 'Menu title', 'upload_files', 'url slug', 'function name to be executed', $logo , 1 );

因此,您只需将“upload_files”替换为与所选角色匹配的任何其他功能。

于 2022-01-15T23:17:40.560 回答
0

add_submenu_page 函数 - 您应该用于“高级设置”的定义如下:

add_submenu_page(字符串$parent_slug,字符串$page_title,字符串$menu_title,字符串$capability,字符串$menu_slug,可调用$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.";
}
于 2017-06-27T13:27:53.853 回答