remove_submenu_page()WP 3.1.0

Remove an admin submenu.

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')

No Hooks.

Return

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

Usage

remove_submenu_page( $menu_slug, $submenu_slug );
$menu_slug(string) (required)
The slug for the parent menu.
$submenu_slug(string) (required)
The slug of the submenu.

Examples

0

#1 Let's remove the page link to the Settings->Permanent Links page:

remove_submenu_page( 'options-general.php', 'options-permalink.php' );

Since the function is added in version 3.1, we may need to make it compatible with earlier versions of the WP, we can organize it this way by adding code to functions.php:

add_action( 'admin_menu', 'my_remove_menu_pages' );

function my_remove_menu_pages() {

	// WP 3.1+
	if ( function_exists( 'remove_menu_page' ) ) {
		remove_submenu_page( 'options-general.php', 'options-discussion.php' );
	}
	// WP below 3.1
	else {
		unset( $GLOBALS['submenu']['options-general.php'][25] );
	}
}

In this example, we removed the Discussion sub-item from the settings menu:

0

#2 An array of menu items

Elements from this array must be specified in this function:

$menu_slug is the key of the array.
$submenu_slug is the third element of the array.

Array (
	[index.php] => Array
			[0] => Array
					[0] => Home
					[1] => read
					[2] => index.php

			[10] => Array
					[0] => Updates <span class='update-plugins count-0'><span class='update-count'>0</span></span>
					[1] => update_core
					[2] => update-core.php

	[upload.php] => Array
			[5] => Array
					[0] => Library
					[1] => upload_files
					[2] => upload.php

			[10] => Array
					[0] => Add new
					[1] => upload_files
					[2] => media-new.php

	[edit-comments.php] => Array
			[0] => Array
					[0] => All comments
					[1] => edit_posts
					[2] => edit-comments.php

	[edit.php] => Array
			[5] => Array
					[0] => All posts
					[1] => edit_posts
					[2] => edit.php

			[10] => Array
					[0] => Add post
					[1] => edit_posts
					[2] => post-new.php

			[15] => Array
					[0] => categories
					[1] => manage_categories
					[2] => edit-tags.php?taxonomy=category

			[16] => Array
					[0] => tags
					[1] => manage_post_tags
					[2] => edit-tags.php?taxonomy=post_tag

	[edit.php?post_type=page] => Array
			[5] => Array
					[0] => All pages
					[1] => edit_pages
					[2] => edit.php?post_type=page

			[10] => Array
					[0] => Add new
					[1] => edit_pages
					[2] => post-new.php?post_type=page

	[themes.php] => Array
			[5] => Array
					[0] => Topics
					[1] => switch_themes
					[2] => themes.php

			[6] => Array
					[0] => Configure
					[1] => customize
					[2] => customize.php?return=%2Fwp-admin%2F
					[3] =>
					[4] => hide-if-no-customize

			[7] => Array
					[0] => Widgets
					[1] => edit_theme_options
					[2] => widgets.php

			[10] => Array
					[0] => Menu
					[1] => edit_theme_options
					[2] => nav-menus.php

	[plugins.php] => Array
			[5] => Array
					[0] => Installed
					[1] => activate_plugins
					[2] => plugins.php

			[10] => Array
					[0] => Add new
					[1] => install_plugins
					[2] => plugin-install.php

	[users.php] => Array
			[5] => Array
					[0] => All users
					[1] => list_users
					[2] => users.php

			[10] => Array
					[0] => Add new
					[1] => create_users
					[2] => user-new.php

			[15] => Array
					[0] => Your profile
					[1] => read
					[2] => profile.php

	[tools.php] => Array
			[5] => Array
					[0] => All instruments
					[1] => edit_posts
					[2] => tools.php

			[10] => Array
					[0] => Import
					[1] => import
					[2] => import.php

			[15] => Array
					[0] => Export
					[1] => export
					[2] => export.php

	[options-general.php] => Array
			[10] => Array
					[0] => General
					[1] => manage_options
					[2] => options-general.php

			[15] => Array
					[0] => Writing
					[1] => manage_options
					[2] => options-writing.php

			[20] => Array
					[0] => Reading
					[1] => manage_options
					[2] => options-reading.php

			[30] => Array
					[0] => Media files
					[1] => manage_options
					[2] => options-media.php

			[40] => Array
					[0] => Permanent Links
					[1] => manage_options
					[2] => options-permalink.php

			[45] => Array
					[0] => Privacy
					[1] => manage_privacy_options
					[2] => privacy.php
)

Notes

  • Global. Array. $submenu

Changelog

Since 3.1.0 Introduced.

remove_submenu_page() code WP 6.5.2

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;
}