函数文档

remove_submenu_page()

💡 云策文档标注

概述

remove_submenu_page() 函数用于从 WordPress 管理后台移除指定的子菜单项。它接受父菜单和子菜单的 slug 作为参数,并返回被移除的子菜单数组或 false。

关键要点

  • 函数签名:remove_submenu_page( $menu_slug, $submenu_slug ),其中 $menu_slug 是父菜单的 slug,$submenu_slug 是子菜单的 slug。
  • 返回值:成功时返回被移除的子菜单数组,失败时返回 false。
  • 移除子菜单可能无法阻止用户直接访问对应屏幕,需结合权限过滤使用。
  • 在较新 WordPress 版本中移除 themes.php 的子菜单(如 theme-editor.php)时,需将 admin_menu hook 的优先级设为较高(如 110),避免使用 admin_init。
  • 子菜单 slug 可能包含 HTML 编码字符(如查询参数),需传递编码后的版本或使用 htmlspecialchars() 处理。

代码示例

remove_submenu_page( 'themes.php', 'nav-menus.php' )
remove_submenu_page( 'tools.php', 'plugin_submenu_slug' )
remove_submenu_page( 'plugin_menu_slug', 'plugin_submenu_slug' )

注意事项

  • 调试时可打印全局 $submenu 数组以确定正确的 slug 组合。
  • 注意 $submenu 变量的可用性,有时需通过 $GLOBALS['submenu'] 访问。
  • 参考管理菜单文档获取标准菜单和子菜单的 slug 列表。

📄 原文内容

Removes an admin submenu.

Description

Example usage:

  • remove_submenu_page( 'themes.php', 'nav-menus.php' )
  • remove_submenu_page( 'tools.php', 'plugin_submenu_slug' )
  • remove_submenu_page( 'plugin_menu_slug', 'plugin_submenu_slug' )

Parameters

$menu_slugstringrequired
The slug for the parent menu.
$submenu_slugstringrequired
The slug of the submenu.

Return

array|false The removed submenu on success, false if not found.

More Information

Depending on when this function is called, it may not prevent users from accessing the screen for the removed submenu directly (see ticket #18850). Removing a menu does not replace the need to filter a user’s permissions as appropriate.

In order to remove the theme-editor.php submenu of themes.php (and others) in more recent versions of WordPress, you need to bind to the admin_menu hook with a very high priority (about 110, depending on the submenus to remove). Don’t use admin_init as previously stated here, hence it will break wp-admin/admin-ajax.php.

Source

function remove_submenu_page( $menu_slug, $submenu_slug ) {
	global $submenu;

	if ( ! isset( $submenu[ $menu_slug ] ) ) {
		return false;
	}

	foreach ( $submenu[ $menu_slug ] as $i => $item ) {
		if ( $submenu_slug === $item[2] ) {
			unset( $submenu[ $menu_slug ][ $i ] );
			return $item;
		}
	}

	return false;
}

Changelog

Version Description
3.1.0 Introduced.

User Contributed Notes

  1. Skip to note 5 content

    Sometimes it can be tricky to figure what combination of menu/submemiu slug is required to remove a submenu.

    You can figure it out by actually printing the global $submenu array to your debug.log file, and identifying the submenu you want to remove.

     global $submenu;
     error_log(print_r($submenu, true)); //this will print out all the menus/submenus currently available in the admin.

  2. Skip to note 6 content

    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

  3. Skip to note 7 content

    Example
    Removes the Widgets submenu page.

    /**
     * Remove the Widgets submenu page.
     */
    function wpdocs_adjust_the_wp_menu() {
    	$page = remove_submenu_page( 'themes.php', '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
    }
    add_action( 'admin_menu', 'wpdocs_adjust_the_wp_menu', 999 );

    In the above example, the value of $page would have been:

    array(3) { [0]=> string(7) "Widgets" [1]=> string(18) "edit_theme_options" [2]=> string(11) "widgets.php" }

  4. Skip to note 8 content

    The submenu slug may be HTML-encoded. Some automatically generated submenu slugs contain multiple query parameters. For example, if one adds categories to a custom post type “video”, the submenu slug will be edit-tags.php?taxonomy=category&post;_type=video.

    This is stored in the $submenu array entry as edit-tags.php?taxonomy=category&post_type=video.

    You must pass the encoded version to remove_submenu_page(), or else it will not find a match.

    You can handle this programmatically by using htmlspecialchars() on your submenu slugs before calling this function. Consider setting the double_encode parameter to false if there’s any chance your string will have been encoded already.

    (This is a pain to debug in the browser, since HTML entities get rendered! Use XDebug or a server-side log to ensure you see the true slug values in $submenu)