Parameter $menu_slug is the slug of the menu, typically the name of the PHP script for the built in menu items; example: edit-comments.php.
This function should be called on the admin_menu action hook. Calling it elsewhere might cause issues: either the function is not defined or the global $menu variable used by this function is not yet declared.
You need to use the right hooks (which are not always the same as the URLs/slugs), and it doesn’t hurt to use a hook that runs later (e.g., admin_init):
This gives you array of the menu positions and inside positions the array of the active menu items and you just need to pickup the right hook (key [2]) and under that is your target hook or URI.
Removes every menu for all users. To remove only certain menu items include only those you want to hide within the function. To remove menus for only certain users you may want to utilize current_user_can() .
Based on the great previous contributions I have developed a script to create the hook functions that will remove all menus. Then I can choose which pages to show, commenting or removing the lines.
You need to place it in functions.php and load wp-admin. Then copy the output to functions.php and remove the script.
add_action( 'admin_init', function () {
echo "add_action( 'admin_init', function () {<br>";
foreach ( $GLOBALS['menu'] as $menu ) {
echo " remove_menu_page( '$menu[2]' );<br>";
}
echo "}, PHP_INT_MAX );";
exit();
} );
Most examples on this Documentation are wrong.
The Hook clearly expects: This function should be called on the admin_menu action hook. Calling it elsewhere might cause issues: either the function is not defined or the global $menu variable used but this function is not yet declared.
So calling it at admin_init like most of these examples here do, will throw: Invalid argument supplied for foreach() in /wp-admin/includes/plugin.php on line 1754
If hooking into admin_init is necessary for some plugins like one of the examples states, that plugin is doing it wrong and should be contacted, not the code hacked to match its wrongdoings.
Some of these plugins may be doing that intentionally because they don’t want their menu item removed.
After spending an hour attempting to debug an error with remove_menu_page, this comment on the end should be on the top. I was calling it with admin_init, which was incorrect. While it actually worked, it worked slowly and caused my error logs to pile up. admin_menu is absolutely correct. My thanks.
You need to use the right hooks (which are not always the same as the URLs/slugs), and it doesn’t hurt to use a hook that runs later (e.g., admin_init):
You can use the following to debug:
This gives you array of the menu positions and inside positions the array of the active menu items and you just need to pickup the right hook (key [2]) and under that is your target hook or URI.
Here is one helper for this:
Just use it like this:
This helper search by name, slug, uri and remove proper menu page.
Example
Removes every menu for all users. To remove only certain menu items include only those you want to hide within the function. To remove menus for only certain users you may want to utilize current_user_can() .
(*) Better to add this priority if dealing with jetpack menu: add_action( ‘admin_menu’, ‘remove_menus’, 999 );
Based on the great previous contributions I have developed a script to create the hook functions that will remove all menus. Then I can choose which pages to show, commenting or removing the lines.
You need to place it in functions.php and load wp-admin. Then copy the output to functions.php and remove the script.
Most examples on this Documentation are wrong.
The Hook clearly expects:
This function should be called on the admin_menu action hook. Calling it elsewhere might cause issues: either the function is not defined or the global $menu variable used but this function is not yet declared.
So calling it at
admin_init
like most of these examples here do, will throw:Invalid argument supplied for foreach() in /wp-admin/includes/plugin.php on line 1754
If hooking into
admin_init
is necessary for some plugins like one of the examples states, that plugin is doing it wrong and should be contacted, not the code hacked to match its wrongdoings.admin_init
, which was incorrect. While it actually worked, it worked slowly and caused my error logs to pile up.admin_menu
is absolutely correct. My thanks.Note that
'admin_menu'
may not be sufficient for certain menus. I’ve experienced this with a few plugins. Alternatively you can use'admin_init'
.Reference: Administration Menus
MENU.................KEY...LINK URL
Dashboard.............2.....index.php
-Home.................0.....index.php
Separator (first).....4.....separator1
Posts.................5.....edit.php
-All Posts............5.....edit.php
-Add New..............10....post-new.php
-Categories...........15....edit-tags.php?taxonomy=category
-Tags.................16....edit-tags.php?taxonomy=post_tag
Media.................10....upload.php
-Library..............5.....upload.php
-Add New..............10....media-new
Links.................15....link-manager.php
-All Links............5.....link-manager.php
-Add New..............10....link-add.php
-Link Categories......15....edit-tags.php?taxonomy=link_category
Pages.................20....edit.php?post_type=page
-All Pages............5.....edit.php?post_type=page
-Add New..............10....post-new.php?post_type=page
Comments..............25....edit-comments.php
-All Comments.........0.....edit-comments.php
Separator (Second)....59....separator2
Appearance............60....themes.php
-Themes...............5.....themes.php
-Widgets..............7.....widgets.php
-Menus................10....nav-menus.php
Plugins...............65....plugins.php
-Installed Plugins....5.....plugins.php
-Add New..............10....plugin-install.php
-Editor...............15....plugin-editor.php
Users.................70....users.php
-All Users............5.....users.php
-Add New..............10....user-new.php
-Your Profile.........15....profile.php
Tools.................75....tools.php
-Available Tools......5.....tools.php
-Import...............10....import.php
-Exports..............15....export.php
Settings..............80....options-general.php
-General..............10....options-general.php
-Writing..............15....options-writing.php
-Reading..............20....options-reading.php
-Discussion...........25....options-discussion.php
-Media................30....options-media.php
-Privacy..............35....options-privacy.php
-Permalinks...........40....options-permalink.php
Separator (last)......99....separator-last
Source: Matt Whiteley