自定义站点角色无法访问外观菜单

时间:2017-05-31 作者:Ryan Coolwebs

我在WordPress多站点中创建了一个名为“站点所有者”的新角色。其目的是为了在子站点中非常接近管理员角色,除非它不能访问插件、设置、工具和外观部分下的一些特定选项。然而,当我按照概述的适当约定注册角色时https://codex.wordpress.org/Function_Reference/add_role 无论我做什么,它似乎都不允许自定义“站点所有者”角色查看任何外观设置。似乎创建的任何自定义角色都无法访问外观。代码如下:

<?php

$result = add_role(
    \'org_site_owner\',
    __( \'Site Owner\' ),
    array(
        \'activate_plugins\'      => false,
        \'create_users\'          => true,
        \'customize\'             => true,
        \'delete_others_pages\'   => true,
        \'delete_others_posts\'   => true,
        \'delete_pages\'          => true,
        \'delete_plugins\'        => false,
        \'delete_posts\'          => true,
        \'delete_private_pages\'  => true,
        \'delete_private_posts\'  => true,
        \'delete_published_pages\'    => true,
        \'delete_published_posts\'    => true,
        \'delete_site\'           => false,
        \'delete_themes\'         => false,
        \'delete_users\'          => true,
        \'edit_dashboard\'        => false,
        \'edit_files\'            => true,
        \'edit_others_pages\'     => true,
        \'edit_others_posts\'     => true,
        \'edit_pages\'            => true,
        \'edit_plugins\'          => false,
        \'edit_posts\'            => true,
        \'edit_private_pages\'    => true,
        \'edit_private_posts\'    => true,
        \'edit_published_pages\'  => true,
        \'edit_published_posts\'  => true,
        \'edit_theme_options\'    => true,
        \'edit_themes\'           => false,
        \'edit_users\'            => true,
        \'export\'                => false,
        \'import\'                => false,
        \'install_plugins\'       => false,
        \'install_themes\'        => false,
        \'list_users\'            => true,
        \'manage_categories\'     => true,
        \'manage_links\'          => true,
        \'manage_options\'        => true,
        \'moderate_comments\'     => true,
        \'promote_users\'         => false,
        \'publish_pages\'         => true,
        \'publish_posts\'         => true,
        \'read\'                  => true,
        \'read_private_pages\'    => true,
        \'read_private_posts\'    => true,
        \'remove_users\'          => true,
        \'switch_themes\'         => false,
        \'unfiltered_html\'       => false,
        \'update_core\'           => false,
        \'update_plugins\'        => false,
        \'update_themes\'         => false,
        \'upload_files\'          => true,
        \'upload_plugins\'        => false,
        \'upload_themes\'         => false,
    )
);

add_action( \'admin_init\' , \'org_site_owner_menu_pages\' );
function org_site_owner_menu_pages()
{
    if ( current_user_can( \'qut_site_owner\' ) )
    {
        remove_menu_page(\'plugins.php\'); // Plugins
        remove_menu_page(\'tools.php\'); // Tools
        remove_menu_page(\'edit.php?post_type=acf\'); // ACF Fields
        remove_menu_page(\'admin.php?page=searchandfilter-settings\'); // Search and filter plugin
        //remove_menu_page( \'themes.php\' ); //Appearance
        remove_submenu_page(\'themes.php\', \'themes.php\'); // hide theme selection

    }
}
如果我将所有数组选项设置为“true”,则根本不会影响它。唯一有效的是remove_menu_page() 底部的选项。这是我无法使用的核心嵌入式WordPress功能吗?

1 个回复
SO网友:Ryan Coolwebs

WP自定义站点角色充其量看起来有点“古怪”,但这个特定的解决方案对我很有效(为它创建了一个自定义插件)。

$result = add_role(
    \'org_site_owner\',
    __( \'Site Owner\' ),
    array(
        \'activate_plugins\'      => false,
        \'create_users\'          => true,
        \'customize\'             => true,
        \'delete_others_pages\'   => true,
        \'delete_others_posts\'   => true,
        \'delete_pages\'          => true,
        \'delete_plugins\'        => false,
        \'delete_posts\'          => true,
        \'delete_private_pages\'  => true,
        \'delete_private_posts\'  => true,
        \'delete_published_pages\'    => true,
        \'delete_published_posts\'    => true,
        \'delete_site\'           => false,
        \'delete_themes\'         => false,
        \'delete_users\'          => true,
        \'edit_dashboard\'        => false,
        \'edit_files\'            => true,
        \'edit_others_pages\'     => true,
        \'edit_others_posts\'     => true,
        \'edit_pages\'            => true,
        \'edit_plugins\'          => false,
        \'edit_posts\'            => true,
        \'edit_private_pages\'    => true,
        \'edit_private_posts\'    => true,
        \'edit_published_pages\'  => true,
        \'edit_published_posts\'  => true,
        \'edit_theme_options\'    => true,
        \'edit_themes\'           => false,
        \'edit_users\'            => true,
        \'export\'                => false,
        \'import\'                => false,
        \'install_plugins\'       => false,
        \'install_themes\'        => false,
        \'list_users\'            => true,
        \'manage_categories\'     => true,
        \'manage_links\'          => true,
        \'manage_options\'        => true,
        \'moderate_comments\'     => true,
        \'promote_users\'         => false,
        \'publish_pages\'         => true,
        \'publish_posts\'         => true,
        \'read\'                  => true,
        \'read_private_pages\'    => true,
        \'read_private_posts\'    => true,
        \'remove_users\'          => true,
        \'switch_themes\'         => false,
        \'unfiltered_html\'       => false,
        \'update_core\'           => false,
        \'update_plugins\'        => false,
        \'update_themes\'         => false,
        \'upload_files\'          => true,
        \'upload_plugins\'        => false,
        \'upload_themes\'         => false,
    )
);

// Allow site owners to see Appearance and Settings menus
$role_object = get_role( \'org_site_owner\' );
$role_object->add_cap( \'edit_theme_options\' );
$role_object->add_cap( \'manage_options\' );

function hide_appearance_options() {
    // Hide theme selection page
    remove_submenu_page( \'themes.php\', \'themes.php\' );
    // Hide media settings page
    remove_submenu_page( \'options-general.php\', \'options-media.php\' );
    // Hide permalinks
    remove_submenu_page( \'options-general.php\', \'options-permalink.php\' );
    // Hide featured image column
    remove_submenu_page( \'options-general.php\', \'featured-image-column\' );
    //Hide SSO/SAML configuration settings
    remove_submenu_page( \'options-general.php\', \'onelogin_saml_configuration\' );
}

function org_site_owner_menu_pages()
{
    $user_role = wp_get_current_user();
    $allowed_roles = array(\'org_site_owner\');
    if( array_intersect($allowed_roles, $user_role->roles ) )
        {
        remove_menu_page(\'plugins.php\'); // Plugins
        remove_menu_page(\'tools.php\'); // Tools
        remove_menu_page(\'edit.php?post_type=acf\'); // ACF Fields
        remove_menu_page(\'admin.php?page=searchandfilter-settings\'); // Search and filter plugin
        add_action(\'admin_head\', \'hide_appearance_options\');
    }
}
add_action( \'admin_init\' , \'org_site_owner_menu_pages\' );

结束

相关推荐