Ste*_*ene 4 wordpress woocommerce
我想按用户角色隐藏特定的 woocommerce 设置选项卡。不是整个子菜单,而只是一个选项卡(具体请查看)。我希望商店经理能够访问大部分设置,但无法影响结账设置。
我怎样才能实现这个目标?
如果您想删除选项卡而不是使用 CSS 隐藏它们,那么您可以将以下内容添加到您的主题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;
}
Run Code Online (Sandbox Code Playgroud)
这使用 WooCommerce 'woocommerce_settings_tabs_array' 过滤器。有关所有 WooCommerce 过滤器和挂钩的更多信息,您可以查看此处: https: //docs.woocommerce.com/wc-apidocs/hook-docs.html
这样做的额外好处是它不再位于 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;
}
Run Code Online (Sandbox Code Playgroud)
这与代码的第一位几乎相同,除了数组的结构不同并且我添加了一个 foreach 之外。foreach 遍历我们想要阻止的选项卡列表,挂钩到用于显示设置页面的“woocommerce_settings_{$tab}”操作。
然后我创建了一个redirect_from_tab_page 函数来将用户重定向到默认的管理URL。这将阻止直接访问不同的设置选项卡。
| 归档时间: |
|
| 查看次数: |
4321 次 |
| 最近记录: |