Post Pic

How to remove menus in WordPress dashboard

Happy 2010! To start this new year with a great WP recipe, I’m going to show you how you can easily remove menus in the WordPress dashboard. This can be really useful when building a WP site for a client.

Simply paste the following code into the functions.php file of your theme. The following example will remove all menus named in the $restricted array.

function remove_menus () {
global $menu;
	$restricted = 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]:"" , $restricted)){unset($menu[key($menu)]);}
	}
}
add_action('admin_menu', 'remove_menus');

Thanks to hungred for this very useful trick! This recipes was previously featured in Cats Who Code 10 WordPress dashboard hacks article.

Enought said, I wish to all of you and your families a wonderful year 2010!

12 Responses

Jul 19 2011 02:59

Great recipe. Thanks for pointing this out!

Jul 19 2011 19:17

You can also go a little more granular using the remove_cap functionality:

Using the editor role specifically:
$editor = get_role(‘editor’);
$editor->remove_cap(‘publish_pages’);

Using global roles:
global $wp_roles;
$wp_roles->remove_cap( ‘editor’, ‘publish_pages’ );

Jul 27 2011 10:10

Great snippet, thanks!

Can i use this code in any way to just remove a sub menu?
Remove Categories and Post Tage under the Posts menu for example?

Thanks!

Aug 22 2011 23:47

Nice trick, I’ve been wondering for a while if this was possible. Thanks for sharing.

Oct 08 2011 01:04

won’t work, must hooked to admin_head instead of admin_menu
code is wrong even on hungred’s post

Oct 28 2011 12:00

The code only hides the menus, doesn’t remove anything. URLs are still accessible.

Nov 17 2011 22:22

Cool snippet, works perfectly with the latest WP. Thanks!

Nov 23 2011 18:26

Great!

Quick question. I have a custom post type called “listings”, but the label is “A to Z”.

I have tried:

$restricted = array(__(‘Listings’)…
and
$restricted = array(__(‘A to Z’)…

and it doesn’t work.

Any tips?

Dec 16 2011 10:23

Hi, I am using the following function to remove some menus in the dashboard.

function remove_menus () {
global $menu;
$restricted = array(__(‘Dashboard’), __(‘Posts’), __(‘Updates’), __(‘Media’), __(‘Links’), __(‘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]:”" , $restricted)){unset($menu[key($menu)]);
}
}
add_action(‘admin_menu’, ‘remove_menus’);

Some of the menus in the array are removerd but Media and Comments menus aren´t. I couldn´t figure out what I am doing wrong.
Any help is very much apreciated.

Dec 18 2011 17:57

@Musti
use like this for media (and others) :

function remove_menu(){
remove_menu_page( ‘upload.php’ );
}
add_action( ‘admin_menu’, ‘remove_menu’, 999 );

Jan 02 2012 21:36

Hi,

I’m not a programmer, but can copy paste code into my functions.php. Here’s my question:

I have two custom post types:

1. Bio (For additional biographic fields)
2. Product (For product detail fields)

I want my admin menu to display the following according to roles:

1. Site Admin: Sees all menus
2. Free Subscriber: Sees only the standard WP Profile menu item in his dashboard.
3. Contributor, Author, Editor: Only see the Profile menu, the custom Bio menu, and the custom Product menu, and nothing else in their dashboard.

I’ve tried various approaches with snippets that work for a part here and other for another part. But I don’t know enough to get it all together.

Help would really be appreciated.

Jan 22 2012 06:56

Thanks for the script it works fine but I need your help to customize the script please. How to apply this script to all users excluding or except userid 1 which would be the superadmin?

In the way the script will hide all the required menus except for the developer and designer.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required