Below is a custom code snippet you can add to your theme’s functions.php
file or as part of a custom plugin. This code will block all WooCommerce tab settings for a specific user role.
Replace 'your_user_role'
with the actual user role you want to block from accessing the WooCommerce tab settings.
function block_woocommerce_tabs_for_role() { // Define the user role you want to block $blocked_role = 'your_user_role'; // Check if the current user has the blocked role if (current_user_can($blocked_role)) { // Remove the WooCommerce settings tabs add_filter('woocommerce_settings_tabs_array', 'remove_woocommerce_tabs', 200); } } function remove_woocommerce_tabs($tabs) { // Unset all WooCommerce tabs foreach ($tabs as $key => $tab) { unset($tabs[$key]); } return $tabs; } add_action('admin_init', 'block_woocommerce_tabs_for_role');
How the Code Works:
block_woocommerce_tabs_for_role
function: Checks if the current user has the specified role. If they do, it adds a filter that removes all WooCommerce settings tabs.
remove_woocommerce_tabs
function: Unsets all the WooCommerce settings tabs so that none are visible to the blocked user role.
admin_init
action hook: Ensures the function runs when the admin panel is initialized.
Customization:
- Replace
'your_user_role'
with the desired user role (e.g., shop_manager
, subscriber
).
- If you want to block specific tabs instead of all, you can modify the
remove_woocommerce_tabs
function to unset only certain tabs by checking the key.
This code will effectively block access to the WooCommerce settings tabs for the specified user role.