我想按用戶角色隱藏特定woocommerce設置選項卡。不是整個子菜單,而只是一個標籤(檢查具體)。 我希望店鋪經理能夠訪問大部分設置,但無法影響結帳設置。隱藏woocommerce設置選項卡
我該如何做到這一點?
我想按用戶角色隱藏特定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;
}
這使用WooCommerce的'woocommerce_settings_tabs_array'過濾器。有關所有WooCommerce過濾器和掛鉤的更多信息,請看這裏:https://docs.woocommerce.com/wc-apidocs/hook-docs.html
這樣做的好處是它不再處於HTML中,因此如果有人查看源代碼,他們將無法找到元素。
您仍然可以訪問這些網址。這只是刪除標籤而不是隱藏它們的一種方式。
編輯: 我已經想出瞭如何停止訪問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_settings _ {$ tab}'操作。
然後我創建了一個redirect_from_tab_page函數來將用戶重定向到默認的管理員URL。這會停止直接訪問不同的設置選項卡。
將這個代碼在你的主題/子主題的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');
風格將輸出到HTML頭只在WP-admin和登錄用戶的角色是shop_manager。
更多關於admin_head鉤,請https://codex.wordpress.org/Plugin_API/Action_Reference/admin_head
是的,這是正確的。好的回答 – LoicTheAztec
非常感謝。這工作完美。 但是,如果他們謙虛地鍵入網址,他們可以訪問該頁面。 我實際上希望能夠阻止他們訪問該頁面。我想你回答了這個問題,雖然 –
感謝你。 我最終使用重定向規則來阻止訪問url本身。 –
看到我更新的答案,我得到了這個使用動作鉤子的工作。在WordPress中使用重定向規則可能會更容易。我想這樣做,因爲我的WordPress是多站點安裝。 –
太好了。我也想用它來進行多站點安裝。 我應該將此答案標記爲已接受 –