You often need to remove or hide menu items from WordPress admin panel or dashboard. This is a common requirement if you develop WordPress websites for clients or manage multi-user blog.
You will learn how to remove menu items from WordPress admin panel. You will also learn the same for submenus. Further, I will also explain how can you hide menu items based on user Id or capability. Please note that all the examples are for WordPress version 3.1 and above.
Hide Menu Items – Requirements and Benefits
Wisely limiting menu items in admin panel not only easier things for clients but also offers numerous advantages. This involves productivity, safety, comfort and control. The next few paragraphs explain them in details if you hide menu items from WordPress admin panel.
You might like: Making mega menu with WordPress and Bootstrap 4
If you’re using a premium theme, it already comes with a number of additional menus and custom post types. You or your client might not always need them. For example, you might need the portfolio, gallery, testimonial or event CPTs but not all on one website. It’s better to remove such unnecessary menu items from WordPress dashboard for a cleaner workspace.
Second, you don’t want every logged in user to play with settings or themes within the admin panel. Hide menu items from them. Keep your work away from them as well as secure the site in this way.
Further, few users complain that WordPress dashboard doesn’t look like a dedicated eCommerce dashboard. If you remove unnecessary menus and widget then you also overcome this complaint heavily.
Hide Menu or Submenu Items
The remove_menu_page() and remove_submenu_page() functions are what we need to remove the menus. Both functions return an array upon success and false
in case the menu doesn’t exist. Check the following scripts below.
Remove a Menu
<?php //Remove a menu function ft_remove_menus(){ remove_menu_page( 'index.php' ); //Dashboard remove_menu_page( 'jetpack' ); //Jetpack* remove_menu_page( 'edit.php' ); //Posts remove_menu_page( 'upload.php' ); //Media remove_menu_page( 'edit.php?post_type=page' ); //Pages remove_menu_page( 'edit-comments.php' ); //Comments remove_menu_page( 'themes.php' ); //Appearance remove_menu_page( 'plugins.php' ); //Plugins remove_menu_page( 'users.php' ); //Users remove_menu_page( 'tools.php' ); //Tools remove_menu_page( 'options-general.php' ); //Settings } add_action( 'admin_menu', 'ft_remove_menus' ); ?>
So here you see all the default menu slugs available in WordPress dashboard. Hide specific menu items which you don’t need in the admin panel. If you’re removing Jetpack menu, pass 999
as the third parameter in the add_action()
hook call for priority.
You have to copy-paste the code snippet in your theme’s function.php file and make necessary changes.
Hide a Submenu
<?php //Remove a submenu function ft_control_submenu() { //remove_submenu_page( $menu_slug, $submenu_slug ); $page = remove_submenu_page( 'themes.php', 'widgets.php' ); /* print_r($page); The above will return: array(3) { [0]=> string(7) "Widgets" [1]=> string(18) "edit_theme_options" [2]=> string(11) "widgets.php" } $page[0] is the menu title $page[1] is the minimum level or capability required $page[2] is the URL to the item's file */ //URL => http://fellowtuts.com/wp-admin/admin.php?page=disqus remove_submenu_page( 'admin.php', 'disqus' ); //URL => http://fellowtuts.com/wp-admin/options-general.php?page=tinymce-advanced remove_submenu_page( 'options-general.php', 'tinymce-advanced' ); } add_action( 'admin_menu', 'ft_control_submenu', 999 ); ?>
Hiding a submenu is little different from hiding a menu item. You require providing parent page slug as well as submenu page slug. Later we have commented the output if you wish to see the returned array.
Make your WordPress website faster using Transient API
Some dashboard URLs contain file name followed by query string parameters. In such case, use the value from page parameter in the URL as shown.
Hide Menu Items Based on User or Capability
Sometimes it requires hiding menu items for a specific user ID or set of IDs. You might even need to remove menus for one among many users with the same capability. The following script demonstrates how can you implement the same.
<?php function ft_remove_custom_menus () { //Remove menus by not allowed user IDs $allowed_ids = array(1, 4, 7, 15); if( !in_array(get_current_user_id(), $allowed_ids) ) { //The user ID is not in the array of IDs, remove menus remove_menu_page( 'some-admin-page.php' ); remove_submenu_page( 'options-general.php', 'certain-plugin-settings' ); } //Remove menus by not allowed user capability if( !current_user_can('install_themes') ) { //Current user can't install theme, remove other menus too } } add_action( 'admin_menu', 'ft_remove_custom_menus', 999 ); ?>
It’s also possible to hide menus by user role. Unfortunately, there is currently no WordPress function to fetch user’s role directly. So you have to first obtain user info then roles and finally check each role. I will publish an article soon to easier this task.
You can compare with email instead if user ID as well. At the end of the article, I have provided some references. I encourage you to read them for alternative or granular selection.
Hide Menu Items from WordPress Admin Panel
Note that removing menus from dashboard doesn’t mean the user can’t access them directly by typing URL in address bar. In other words, removing a menu doesn’t change the user’s capability in the admin panel. You have to play with user roles and capabilities to restrict. However, it’s a broad topic and we will cover it some other day.
From start to finish: WordPress Custom Post Types and Fields
There are few plugins too to control the menus as well as user capabilities. However, as we’re coding guys, we prefer coding than installing and configuring for non-complex requirements. Still, we have listed a few useful plugins below to hide unnecessary items from users in WordPress admin.
Plugins:
Adminimize
User Role Editor
Admin Menu Editor
We haven’t tested either of them. If your requirements are wide or you’re not a coding guy, try these plugins. Have any question or feedback regarding hiding menu items? Comment in the form below and let us assist you.
I have comments deactivated in adminimize in the menu options but it stil shows up on the admin screen for all users. I would like only admins and editors to see the comments.