首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >隐藏woocommerce设置选项卡

隐藏woocommerce设置选项卡
EN

Stack Overflow用户
提问于 2016-04-21 19:58:49
回答 2查看 4K关注 0票数 3

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

我怎样才能做到这一点?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-12-15 12:42:16

如果要删除选项卡而不是使用CSS隐藏它们,则可以将以下内容添加到主题functions.php中:

代码语言:javascript
运行
复制
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_settings_tabs_array‘过滤器。有关所有WooCommerce过滤器和钩子的更多信息,您可以在这里查看:https://docs.woocommerce.com/wc-apidocs/hook-docs.html

这有一个额外的好处,即它不再存在于HTML中,因此,如果有人查看源代码,他们将找不到元素。

您仍然可以访问URL。这只是一种删除标签而不是隐藏标签的方法。

编辑:我想出了如何停止对URL的访问。复制以下内容:

代码语言:javascript
运行
复制
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会遍历我们要阻止的选项卡列表,将其钩子到“woocommerce_settings_{$tab}”操作中,该操作用于显示设置页。

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

票数 5
EN

Stack Overflow用户

发布于 2016-04-22 00:34:58

将此代码放入主题/子主题functions.php或其他地方:

代码语言:javascript
运行
复制
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');

样式只会在wp中输出到html头,登录用户角色是shop_manager。

有关admin_head钩子的更多信息,请查看head

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36779307

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档