Wordpress 隐藏设置选项卡

Wordpress 隐藏设置选项卡,wordpress,woocommerce,Wordpress,Woocommerce,我想按用户角色隐藏特定的设置选项卡。不是整个子菜单,而是一个选项卡(具体来说是签出)。 我希望商店经理能够访问大多数设置,但不能影响结帐设置 如何实现这一点?将此代码放在theme/child-theme-functions.php或其他地方: if (!function_exists('hide_setting_checkout_for_shop_manager')){ function hide_setting_checkout_for_shop_manager() {

我想按用户角色隐藏特定的设置选项卡。不是整个子菜单,而是一个选项卡(具体来说是签出)。 我希望商店经理能够访问大多数设置,但不能影响结帐设置


如何实现这一点?

将此代码放在theme/child-theme-functions.php或其他地方:

if (!function_exists('hide_setting_checkout_for_shop_manager')){
    function hide_setting_checkout_for_shop_manager() {

        $user = wp_get_current_user();
        //check if user is shop_manager
        if ( isset( $user->roles[0] ) && $user->roles[0] == 'shop_manager' ) {
            echo '<style> .woocommerce_page_wc-settings  form .woo-nav-tab-wrapper a[href="'.admin_url('admin.php?page=wc-settings&tab=checkout').'"]{ display: none; } </style>';
        }

    }
}
add_action('admin_head', 'hide_setting_checkout_for_shop_manager');
如果(!function_存在('hide_setting_checkout_for_shop_manager')){
函数隐藏\u设置\u结帐\u用于\u商店\u管理器(){
$user=wp_get_current_user();
//检查用户是否为车间经理
如果(isset($user->roles[0])&&&$user->roles[0]=='shop\u manager'){
echo'.woocommerce_page_wc-settings form.woo导航选项卡包装器a[href=“'.admin_url('admin.php?page=wc settings&tab=checkout')。“]{display:none;}”;
}
}
}
添加操作(“管理员头部”、“隐藏设置”、“结帐”以供店铺经理使用);
样式将仅在wp admin输出到html head,登录用户角色为shop_manager


有关admin_head hook的更多信息,请检查如果您想删除选项卡而不是使用CSS隐藏它们,那么您可以将以下内容添加到您的theme functions.php中:

add_filter( 'woocommerce_settings_tabs_array', 'remove_woocommerce_setting_tabs', 200, 1 );
function remove_woocommerce_setting_tabs( $tabs ) {
    // Declare the tabs we want to hide
    $tabs_to_hide = array(
        'Tax',
        'Checkout',
        'Emails',
        'API',
        'Accounts',
        );


    // Get the current user
    $user = wp_get_current_user();

    // Check if user is a shop-manager
    if ( isset( $user->roles[0] ) && $user->roles[0] == 'shop_manager' ) {

        // Remove the tabs we want to hide
        $tabs = array_diff($tabs, $tabs_to_hide);
    }

    return $tabs;
}
这将使用WooCommerce“WooCommerce\u设置\u选项卡\u数组”过滤器。有关所有WooCommerce过滤器和挂钩的更多信息,请查看:

这只是增加了一个好处,即它不再出现在HTML中,因此如果有人查看源代码,他们将找不到元素

您仍然可以访问URL。这只是移除选项卡而不是隐藏它们的一种方法

编辑: 我已经知道如何停止访问URL。复制以下内容:

add_filter( 'woocommerce_settings_tabs_array', 'remove_woocommerce_setting_tabs', 200, 1 );
function remove_woocommerce_setting_tabs( $array ) {
    // Declare the tabs we want to hide
    $tabs_to_hide = array(
        'tax' => 'Tax',
        'checkout' => 'Checkout',
        'email' => 'Emails',
        'api' => 'API',
        'account' => 'Accounts',
        );

    // Get the current user
    $user = wp_get_current_user();

    // Check if user is a shop_manager
    if ( isset( $user->roles[0] ) && $user->roles[0] == 'shop_manager' ) {

        // Remove the tabs we want to hide from the array
        $array = array_diff_key($array, $tabs_to_hide);

        // Loop through the tabs we want to remove and hook into their settings action
        foreach($tabs_to_hide as $tabs => $tab_title) {
            add_action( 'woocommerce_settings_' . $tabs , 'redirect_from_tab_page');
        }
    }

    return $array;
}

function redirect_from_tab_page() {
    // Get the Admin URL and then redirect to it
    $admin_url = get_admin_url();
    wp_redirect($admin_url);
    exit;
}
这与第一段代码基本相同,只是数组的结构不同,我添加了一个foreach。foreach遍历我们要阻止的选项卡列表,钩住用于显示设置页面的“woocommerce\u settings{$tab}”操作


然后我创建了一个redirect_from_tab_page函数,将用户重定向到默认的管理员URL。这将停止直接访问不同的设置选项卡。

非常感谢。这非常有效。但是,如果他们直接键入url,则可以访问该页面。我实际上希望能够阻止他们访问该页面。我想你回答了这个问题,谢谢你。我最终使用重定向规则来阻止对URL本身的访问。在WordPress中使用这一切可能比使用重定向规则更容易。我想这样做,因为我的WordPress是一个多站点安装。我还想将其用于多站点安装。我应该把这个答案标记为接受