Remove Menu Item in WordPress Admin Panel

With WordPress Version 3.1 two new functions were added which makes it easier to remove menu- and submenu-entries in WordPress Admin Panel. These functions removing entries of the menu-tree remove_menu_page or submenus - remove_submenu_page.

/**
 * Remove a top level admin menu
 *
 * @param string $menu_slug The slug of the menu
 * @return array|bool The removed menu on success, False if not found
 */
remove_menu_page( $menu_slug )
/**
 * Remove an admin submenu
 *
 * @param string $menu_slug The slug for the parent menu
 * @param string $submenu_slug The slug of the submenu
 * @return array|bool The removed submenu on success, False if not found
 */
remove_submenu_page( $menu_slug, $submenu_slug ) {

It's easy to remove menu entries and it is not necessary anymore to read the arrays $menu and $submenu. Until now you had to search for them in the array and remove them via unset() from the array. Alternatively you were also able to find the entry on the basis of a key - the above new features make this unnecessary and as a parameter value only the "slug" is passed, which can be found in the link or the URL of the backend. A small example, where we remove the entries to the comments and the submenu-page discussion will show the new possibilities.

function fb_remove_menu_entries () {
	// with WP 3.1 and higher
	if ( function_exists( 'remove_menu_page' ) ) {
		remove_menu_page( 'edit-comments.php' );
		remove_submenu_page( 'options-general.php', 'options-discussion.php' );
	} else {
		// unset comments
		unset( $GLOBALS['menu'][25] );
		// unset menuentry Discussion
		unset( $GLOBALS['submenu']['options-general.php'][25] );
	}
}
add_action( 'admin_menu', 'fb_remove_menu_entries' );

The above code provides a simple solution, removed the two entries and also has a fallback for WordPress, smaller version 3.1. It is also conceivable that the removal is also connected with user rights

if ( function_exists( 'remove_menu_page' ) && ! current_user_can( 'manage_options' ) ) {

so it would be possible to optimize the menu explicitly for a user. Alternatively, the plug Adminimize helps and facilitates the job via admin area.
You can also read about this topic on Justin's post, but my post was an older draft and now was published - the topic is worth it.

Comments are closed.

One comment

  1. Maggan

    So if I was to remove say Contact Form 7 admin menu, what would be the correct path to add then? I tried multiple approaches without success. :(