What is the Tutorial About?
See how to hide specific items on the WordPress sidebar for different user roles using a free Code Snippet. This method, explained by Web Squadron, simplifies the process, eliminates the need for a child theme, and allows for easy rollback in case of any issues. By customizing the sidebar for user roles, you can improve the user experience and provide a more organized interface for each type of user.
Use Cases for this Tutorial
- Simplify the WordPress dashboard for users with limited permissions, such as authors or contributors, by hiding unnecessary sidebar items
- Streamline the interface for editors, allowing them to focus on content management by hiding settings, appearance, and other non-essential options
- Enhance website security by restricting access to certain dashboard items based on user roles, ensuring that sensitive settings are only accessible to authorized users
Required Code
Source: https://www.codesnippets.cloud/snippet/WebSquadron/Hide-Wordpress-Items-based-on-User-Role
function remove_dashboard_menu_items() {
global $menu;
$user = wp_get_current_user();
$user_role = $user->roles[0]; // Get the first user role assigned to the user
if ($user_role == 'administrator') {
// Admin can see all menu items
return;
} elseif ($user_role == 'editor') {
// Editors can see Pages and Posts
unset($menu[25]); // Removes "Comments"
unset($menu[60]); // Removes "Appearance"
unset($menu[65]); // Removes "Plugins"
unset($menu[70]); // Removes "Users"
unset($menu[75]); // Removes "Tools"
unset($menu[80]); // Removes "Settings"
remove_menu_page('index.php'); // Removes the Dashboard home page
} else {
// All other users see limited menu items
unset($menu[5]); // Removes "Posts"
unset($menu[10]); // Removes "Media"
unset($menu[20]); // Removes "Pages"
unset($menu[25]); // Removes "Comments"
unset($menu[60]); // Removes "Appearance"
unset($menu[65]); // Removes "Plugins"
unset($menu[70]); // Removes "Users"
unset($menu[75]); // Removes "Tools"
unset($menu[80]); // Removes "Settings"
remove_menu_page('index.php'); // Removes the Dashboard home page
}
}
add_action('admin_menu', 'remove_dashboard_menu_items');