How To: Hide an Admin Menu in WordPress


Have you ever needed to hide a specific admin menu from other users in WordPress? Maybe you want to hide the Plugins and Appearance menus to keep your users out of trouble. Just place the below code in your themes functions.php file to hide the Plugins menu from all users except for admin:

<?php
add_action('admin_head', 'hide_menus');

function hide_menus() {
	global $current_user;
	get_currentuserinfo();

	If($current_user->user_login != 'admin') {
		?>
		<style>
		   #menu-plugins{
				display:none;
			}
		</style>
		<?php
	}
}
?>

As another example lets say we want to hide the Links menu from all users that aren’t administrators in WordPress. The below code would do just that:

<?php
add_action('admin_head', 'hide_menus');

function hide_menus() {
	if ( !current_user_can('manage_options') ) {
		?>
		<style>
		   #menu-links{
				display:none;
			}
		</style>
		<?php
	}
}
?>

This is a pretty simple method of hiding menus in the WordPress admin dashboard. Enjoy!

Related posts:

  1. How To: Redirect A User After Logging Into WordPress
  2. How To: Load User Info Using the Admin Email in WordPress
  3. How To: Remove Default Profile Fields in WordPress
  4. How To: Create Backdoor Admin Access in WordPress
  5. How To: Add A Link to the WordPress Multisite Network Admin Sites List
Enjoy this post? Be sure to subscribe to my RSS feed and my WordPress Tips and Tricks Newsletter! Also check out my new book: Professional WordPress

Comments

  1. mac_boy says:

    The CSS method is a good quick-n-dirty approach, sometimes its the only way!

    Another method is to use the WordPress API by un-registering the menus.

    Now-a-days, the Adminimize plugin is all that is necessary. It awesomely handles each role’s access to each element of the admin screens.

    Wonder how Adminimize handles custom panels and metaboxes and taxonomies and custom post types?

    Get Adminimize’d, yo!

    :-)

  2. Bob says:

    I had problems with using admin_init, causing the “headers already sent” error. Probably because its calling the function before WP sets the cookies before HTML output starts. The solution was to change it to admin_head where it inserts the style html within the head section.

  3. Brad says:

    Excellent point Bob, I updated the code to use admin_head.

  4. Ripul says:

    I tried testing this on my local machine. but there are errors when i put the above code in functions.php in my theme.
    I really need this sort of thing to give special privelage to an admin user and not all admin users. Primarily because unlike joomla WP does not have a super admin account. Please help!

  5. Brad says:

    @Ripul can you share the error message you received?

  6. Maor says:

    Well, this is good only for **hiding** the menus, but still not to prevent using it. Since if this user will browse to the edit plugins url (or any other url hided), he will still have the access.
    So that could be a nice method for hiding menus from ordinary users, but it’s not secured if you have a site with a lot of users that for example can edit posts, and they might still access unauthorized areaes.
    un-registering the menus , as mentioned above, is a much better way to deal with.

  7. SWEET.

    thank you so much.

  8. Agree on Bob. Css trick is a bit dirty. But this approach is helpful for someone who wants to save time coding or figuring out a pro grammatically correct solution. Thanks for a great post!

  9. Ali Nasir says:

    good article. Thanks for posting, i have tried that and works well.

  10. eric says:

    Thanks. I’ve been looking everywhere for this.

  11. Kevin Chard says:

    For clients I use a combo of the “role manager plugin” and the method bellow to remove menu and submenu items.

    add_action( ‘admin_init’, ‘my_remove_menu_pages’ );
    function my_remove_menu_pages() {
    global $menu;
    global $current_user;
    get_currentuserinfo();

    if($current_user->user_login != ‘ADMINNAME’){
    remove_menu_page(‘edit.php’);
    remove_submenu_page(‘users.php’,'role-management.php’);
    }
    }

    For one project I wanted to remove the listing of some pages to stop clients from editing them. I used a method similar to yours to get this result. Each tr tag in the post or page listing has an id=”post-179″ so I removed pages i wanted to hide from them.

    Great blog lots of great information.

  12. Marco Famà says:

    Thanks for sharing this easy workflow!
    That’s what I needed!! :)

    Marco

Speak Your Mind

*