remove_menu_page( string $menu_slug )

Remove a top-level admin menu.


Parameters Parameters

$menu_slug

(string) (Required) The slug of the menu.


Top ↑

Return Return

(array|bool) The removed menu on success, false if not found.


Top ↑

More Information More Information

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 but this function is not yet declared.

To remove submenu items in the admin, use remove_submenu_page. Using remove_menu_page() will not work for submenu items.

 


Top ↑

Source Source

File: wp-admin/includes/plugin.php

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

Top ↑

Changelog Changelog

Changelog
Version Description
3.1.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by 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 );

  2. Skip to note 2 content

    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.

  3. Skip to note 3 content
    Contributed by 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' );
    
  4. Skip to note 4 content

    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 "&nbsp;&nbsp;&nbsp;&nbsp;remove_menu_page( '$menu[2]' );<br>";
    	}
    
    	echo "}, PHP_INT_MAX );";
    	exit();
    } );
    

You must log in before being able to contribute a note or feedback.