Changing the Default WordPress Admin Menu

Published by John on June 15, 2012 Under Wordpress

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!

See this post if you want to remove the top menu items, like the top right profile and logout links.

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;	
}

Explanation of WordPress Plugin Code

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

  • Line 1 -6: Check users permissions and add hook to the “admin_menu” , calling the
  • Line 3: Optional, remove admin bar for non-administrators. This is bar visible on public pages.
  • Line 12: For debugging, you can uncomment this and it will print the two global variables, so you can see their structure.
  • Line 15: $good_menu_items, an array of accepted menu items. In this case, the only good one is ‘Profile’.
  • Line 16: $new_menu_details, a multi-dimensional array. Set the key, in this case ‘Profile’ to the name of menu. Then, add array with ‘name’ and ‘order’ key to override. Note that both must be set, although that could be changed.
    Line 18: $clean_menu_items, an array to hold new menu items.
  • Line 20-38: Loop through menu items, check if ‘menu_item_name’ corresponds to an element in the good_menu_items array. Check the new_menu_details array, for a different name and menu order.
  • Line 40: update ‘menu’ with new menu items.

Before Using in Production

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.


No Comments |

Add a Comment