remove_menu_page()
云策文档标注
概述
remove_menu_page() 函数用于移除 WordPress 后台的顶级管理菜单。它通过匹配菜单 slug 来操作全局 $menu 数组,应在 admin_menu 钩子上调用以确保正确执行。
关键要点
- 函数功能:移除顶级管理菜单,返回被移除的菜单数组或 false(如果未找到)。
- 参数:$menu_slug(字符串,必需),菜单的 slug,通常是内置菜单项的 PHP 脚本名称(如 edit-comments.php)。
- 调用时机:必须在 admin_menu 动作钩子上调用,否则可能导致函数未定义或全局 $menu 变量未声明的问题。
- 限制:仅适用于顶级菜单,移除子菜单项应使用 remove_submenu_page()。
- 版本历史:自 WordPress 3.1.0 引入。
代码示例
// 移除工具菜单的示例
remove_menu_page( 'tools.php' );
// 在 admin_menu 钩子上批量移除菜单的示例
function wpdocs_remove_menus() {
remove_menu_page( 'index.php' ); // 仪表盘
remove_menu_page( 'edit.php' ); // 文章
remove_menu_page( 'edit-comments.php' ); // 评论
// 更多菜单移除...
}
add_action( 'admin_menu', 'wpdocs_remove_menus' );注意事项
- 避免在 admin_init 钩子上调用 remove_menu_page(),这可能导致错误或性能问题,除非特定插件要求(但这是非标准做法)。
- 调试菜单 slug 时,可以输出 $GLOBALS['menu'] 数组来查看当前菜单项和对应的 slug(键 [2])。
- 用户贡献笔记中提供了辅助函数和更多示例,但需注意钩子选择的正确性。
原文内容
Removes a top-level admin menu.
Description
Example usage:
remove_menu_page( 'tools.php' )remove_menu_page( 'plugin_menu_slug' )
Parameters
$menu_slugstringrequired-
The slug of the menu.
Source
function remove_menu_page( $menu_slug ) {
global $menu;
foreach ( $menu as $i => $item ) {
if ( $menu_slug === $item[2] ) {
unset( $menu[ $i ] );
return $item;
}
}
return false;
}
Changelog
| Version | Description |
|---|---|
| 3.1.0 | Introduced. |
Skip to note 7 content
Ivijan-Stefan Stipic
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):
add_action( 'admin_init', function () { remove_menu_page( 'edit.php?post_type=participants-database' ); remove_menu_page( 'wpcf7' ); });You can use the following to debug:
add_action( 'admin_init', function () { echo '<pre>' . print_r( $GLOBALS[ 'menu' ], true) . '</pre>'; } );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:
if(!function_exists('remove_admin_page')) { function remove_admin_page($needle) { if(isset($GLOBALS[ 'menu' ]) && !empty($GLOBALS[ 'menu' ]) && !empty($needle)) { $needle = strtolower($needle); $needle = trim($needle); foreach($GLOBALS[ 'menu' ] as $position => $items) { foreach($items as $key => $item) { if(strtolower($item) == $needle) { remove_menu_page( $items[2] ); } } } } } }Just use it like this:
remove_admin_page( 'Participants Database' );This helper search by name, slug, uri and remove proper menu page.
Skip to note 8 content
Codex
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() .
/** * Removes some menus by page. */ function wpdocs_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', 'wpdocs_remove_menus' ); ?>(*) Better to add this priority if dealing with jetpack menu: add_action( ‘admin_menu’, ‘remove_menus’, 999 );
Skip to note 9 content
Anonymous User
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_initlike most of these examples here do, will throw:Invalid argument supplied for foreach() in /wp-admin/includes/plugin.php on line 1754If hooking into
admin_initis 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_menuis absolutely correct. My thanks.Skip to note 10 content
Ricardo Gonçalves
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(); } );Skip to note 11 content
cjstage
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'.function wpdocs_remove_menus(){ remove_menu_page( 'some-plugin' ); //Some Plugin Page } add_action( 'admin_init', 'wpdocs_remove_menus' );Skip to note 12 content
Andrea Alba
Reference: Administration Menus
<strong>MENU</strong>.................<strong>KEY</strong>...<strong>LINK URL</strong><br /><strong>Dashboard</strong>.............2.....index.php<br />
-Home.................0.....index.php<br />
<strong>Separator (first)</strong>.....4.....separator1<br />
<strong>Posts</strong>.................5.....edit.php<br />
-All Posts............5.....edit.php<br />
-Add New..............10....post-new.php<br />
-Categories...........15....edit-tags.php?taxonomy=category<br />
-Tags.................16....edit-tags.php?taxonomy=post_tag<br />
<strong>Media</strong>.................10....upload.php<br />
-Library..............5.....upload.php<br />
-Add New..............10....media-new<br />
<strong>Links</strong>.................15....link-manager.php<br />
-All Links............5.....link-manager.php<br />
-Add New..............10....link-add.php<br />
-Link Categories......15....edit-tags.php?taxonomy=link_category<br />
<strong>Pages</strong>.................20....edit.php?post_type=page<br />
-All Pages............5.....edit.php?post_type=page<br />
-Add New..............10....post-new.php?post_type=page<br />
<strong>Comments</strong>..............25....edit-comments.php<br />
-All Comments.........0.....edit-comments.php<br />
<strong>Separator (Second)</strong>....59....separator2<br />
<strong>Appearance</strong>............60....themes.php<br />
-Themes...............5.....themes.php<br />
-Widgets..............7.....widgets.php<br />
-Menus................10....nav-menus.php<br />
<strong>Plugins</strong>...............65....plugins.php<br />
-Installed Plugins....5.....plugins.php<br />
-Add New..............10....plugin-install.php<br />
-Editor...............15....plugin-editor.php<br />
<strong>Users</strong>.................70....users.php<br />
-All Users............5.....users.php<br />
-Add New..............10....user-new.php<br />
-Your Profile.........15....profile.php<br />
<strong>Tools</strong>.................75....tools.php<br />
-Available Tools......5.....tools.php<br />
-Import...............10....import.php<br />
-Exports..............15....export.php<br />
<strong>Settings</strong>..............80....options-general.php<br />
-General..............10....options-general.php<br />
-Writing..............15....options-writing.php<br />
-Reading..............20....options-reading.php<br />
-Discussion...........25....options-discussion.php<br />
-Media................30....options-media.php<br />
-Privacy..............35....options-privacy.php<br />
-Permalinks...........40....options-permalink.php<br />
<strong>Separator (last)</strong>......99....separator-last
Source: Matt Whiteley