How to Limit Dashboard Access in WordPress
If you are running a wordpress site then you must aware about the diffrent user role and their capabilities. When you create a user on the WordPress site, they get access to the WordPress admin area where all the necessary menu items exist. And that admin area is known as dashboard. Here I am going to share you the techniques through which you can limit the role of users to access the dashboard menu items.
Why Limit Dashboard Access?
When we create a user on WordPress site, they have all the control over all menu items available on dashboard eg (plugin.theme, settings, media, etc). So you must divide the capabilities of accessing the dashboard according to the user role.
There are many menu items in the dashboard which are listed below :
- Dashboard
- Posts
- Media
- Links
- Pages
- Appearance
- Plugins
- Tools
- Users
- Settings
List of User Roles in WordPress Site
- Administrator
- Editor
- Author
- Contributor
- Subscriber
Additional User Roles in WooCommerce
When we setup a ecommerce store with the help of woocommerce then two another roles added in the list of users.
- Shop Manager
- Customer
Shop Manager – When you setup a ecommerce store then to manage your store, you have to assign the shop manager role to someone who can control over the store. A shop manager directly enters into your dashboard of WordPress site where all the menu items exist. So you have to restrict him/her to only limited menu of woocommerce .
Hide Woocommerce Setting for Shop Manager
Open your function.php file and use the code which is listed below.
add_filter( 'woocommerce_settings_tabs_array', 'remove_woocommerce_setting_tabs', 200, 1 ); function remove_woocommerce_setting_tabs( $array ) { $tabs_to_hide = array( 'tax' => 'Tax', 'checkout' => 'Checkout', 'email' => 'Emails', 'api' => 'API', 'account' => 'Accounts', 'shipping'=>'Shipping', 'products'=>'Products', 'advanced'=>'Advanced', ); $user = wp_get_current_user(); if ( isset( $user->roles[0] ) && $user->roles[0] == 'shop_manager' ) { $array = array_diff_key($array, $tabs_to_hide); foreach($tabs_to_hide as $tabs => $tab_title) { add_action( 'woocommerce_settings_' . $tabs , 'redirect_from_tab_page'); } } return $array; } function redirect_from_tab_page() { $admin_url = get_admin_url(); wp_redirect($admin_url); exit; }
Hide Dashboard Menu Items for Shop Manager
Open your function.php file and use the code which is listed below.
if (current_user_can('shop_manager')) { function remove_admin_menus () { global $menu; $removed = array( __('Dashboard'), __('Posts'), __('Media'), __('Links'), __('Pages'), __('Appearance'), __('Tools'), __('Users'), __('Settings'), __('Comments'), __('Plugins'), ); end ($menu); while (prev($menu)){ $value = explode( ' ', $menu[key($menu)][0]); if(in_array($value[0] != NULL?$value[0]:"" , $removed)){ unset($menu[key($menu)]); } } } } add_action('admin_menu', 'remove_admin_menus');
Hide Dashboard Menu Items for Other Users
Open your function.php file and use the code which is listed below. You can change the current user role name inside if condition according to your need like to limit administrator, customer, editor.eg
if (current_user_can('administrator'))
if (current_user_can('administrator')) { function remove_admin_menus () { global $menu; $removed = array( __('Dashboard'), __('Posts'), __('Media'), __('Links'), __('Pages'), __('Appearance'), __('Tools'), __('Users'), __('Settings'), __('Comments'), __('Plugins'), ); end ($menu); while (prev($menu)){ $value = explode( ' ', $menu[key($menu)][0]); if(in_array($value[0] != NULL?$value[0]:"" , $removed)){ unset($menu[key($menu)]); } } } } add_action('admin_menu', 'remove_admin_menus');