I am working on a site now that has ties into the WordPress user system. I needed a way of changing the default menus, specifically removing the left hand side ‘Dashboard’ link, renaming the ‘Profile’ link for certain user types, as well as removing most of the other menus.
I found a couple different methods, but didn’t like the solutions I found or they others didn’t quite offer the flexibility I needed, nor did I want to install a plugin for a few lines of php. If you are going to use this on a live, non-production site, read the notes below!
Basic Overview
This function lets you change the left hand wp-admin menu items, restricting it to only a specific set, as well as renaming the items and changing the menu order. In this example, the left-hand user ‘Profile’ link is renamed to ‘Password’.
The basic function, which can be placed in your functions.php, is below:
if (!current_user_can('administrator')){ show_admin_bar(false); add_action( 'admin_menu', 'kcr_clean_dashboard_menu_items', 10000 ); } function kcr_clean_dashboard_menu_items(){ global $menu, $submenu; //echo "MAIN MENU::: "; print_r($menu); echo '<br />Submenu::: '; print_r($submenu); die; $submenu = array(); $good_menu_items = array('Profile'); $new_menu_details = array('Profile'=>array('name'=>'Password', 'order'=>150)); $clean_menu_items = array(); foreach($menu as $menu_order => $menu_item){ $menu_item_order = $menu_order; $menu_item_name = $menu_item[0]; if(in_array($menu_item_name, $good_menu_items)){ //Check for updated order or name if(isset($new_menu_details[$menu_item_name])) { $menu_item[0] = $new_menu_details[$menu_item_name]['name']; $menu_item_order = $new_menu_details[$menu_item_name]['order']; } $clean_menu_items[$menu_item_order] = $menu_item; } } $menu = $clean_menu_items; return; }
Basic Explanation: This function hooks onto the ‘admin_menu’ action, then loops through the global ‘menu’ variable, checking if the menu name is in a list of known good menu items. Next, the menu item is checked against a list of menu items, changing the menu link name and menu order as needed.
Line by Line
Please be careful before using this in production! Note that:
– This does not disable a menu item, only hides it. The menu item could still be accessed directly by a user.
– The menu layout could potentially change, so be careful when upgrading and make sure you test the items. Specifically, I don’t like getting the menu name, from [0] element.
– Disabling “submenu” as above might not be a good idea in all situations, especially if you are using custom post types or user roles. Uncomment the debug line to see/tweak it as needed.
– This is NOT the version I am using, it is simplified. You may want to add additional checks, to verify menu format, layout, ect.
– There might be a better hook to use, however this appears to work fine and similar plugins used the same hook.
Add a Comment