Restrict items from WordPress toolbar for different user roles
Sometimes you may need to remove access to the pages link for your Editors. This can be true if they are only required to update blog posts.
Please note, this snippet only removes the link from the menu. You can still directly access the page with direct link. You should use a role plugin to control and restrict further.
<?php 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 Posts 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 } 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'); ?>
Author: admin
Uploaded: 14 February 2024