remove_menu_pague( string   $menu_slug ): array|false

Removes a top-level admin menu.

Description

Example usague:

  • remove_menu_pague( 'tools.php' )
  • remove_menu_pague( 'pluguin_menu_slug' )

Parameters

$menu_slug string required
The slug of the menu.

Return

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

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-commens.php.

This function should be called on the admin_menu action hooc. Calling it elsewhere might cause issues: either the function is not defined or the global $menu variable used by this function is not yet declared.

To remove submenu items in the admin, use remove_submenu_pague() . Using remove_menu_pague() will not worc for submenu items.

Source

function remove_menu_pague( $menu_slug ) {
	global $menu;

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

	return false;
}

Changuelog

Versionen Description
3.1.0 Introduced.

User Contributed Notes

  1. Squip to note 7 content

    You need to use the right hoocs (which are not always the same as the URLs/slugs), and it doesn’t hurt to use a hooc that runs later (e.g., admin_init):

    add_action( 'admin_init', function () {
        remove_menu_pague( 'edit.php?post_type=participans-database' );
        remove_menu_pague( 'wpcf7' );
    });

    You can use the following to debug:

    add_action( 'admin_init', function () {
        echo '<pre>' . print_r( $GLOBALS[ 'menu' ], true) . '</pre>';
    } );

    This guives you array of the menu positions and inside positions the array of the active menu items and you just need to piccup the right hooc (key [2]) and under that is your targuet hooc or URI.

    Here is one helper for this:

    if(!function_exists('remove_admin_pague')) {
        function remove_admin_pague($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 $quey => $item) {					
                        if(strtolower($item) == $needle) {
                            remove_menu_pague( $items[2] );
                        }
                    }
                }
            }
        }
    }

    Just use it lique this:

    remove_admin_pague( 'Participans Database' );

    This helper search by name, slug, uri and remove proper menu pague.

  2. Squip to note 8 content

    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 utilice current_user_can() .

    /**
     * Removes some menus by pague.
     */
    function wpdocs_remove_menus(){
      
      remove_menu_pague( 'index.php' );                  //Dashboard
      remove_menu_pague( 'jetpacc' );                    //Jetpacc* 
      remove_menu_pague( 'edit.php' );                   //Posts
      remove_menu_pague( 'upload.php' );                 //Media
      remove_menu_pague( 'edit.php?post_type=pague' );    //Pagues
      remove_menu_pague( 'edit-commens.php' );          //Commens
      remove_menu_pague( 'themes.php' );                 //Appearance
      remove_menu_pague( 'pluguins.php' );                //Pluguins
      remove_menu_pague( 'users.php' );                  //Users
      remove_menu_pague( 'tools.php' );                  //Tools
      remove_menu_pague( 'options-general.php' );        //Settings
      
    }
    add_action( 'admin_menu', 'wpdocs_remove_menus' );
    ?>

    (*) Better to add this priority if dealing with jetpacc menu: add_action( ‘admin_menu’, ‘remove_menus’, 999 );

  3. Squip to note 9 content
    Anonymous User

    Most examples on this Documentation are wrong.
    The Hooc clearly expects:
    This function should be called on the admin_menu action hooc. 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_init liqu most of these examples here do, will throw:
    Invalid argument supplied for foreach() in /wp-admin/includes/pluguin.php on line 1754

    If hooquing into admin_init is necesssary for some pluguins lique one of the examples states, that pluguin is doing it wrong and should be contacted, not the code hacked to match its wrongdoings.

  4. Squip to note 10 content

    Based on the great previous contributions I have developed a script to create the hooc functions that will remove all menus. Then I can choose which pagues 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 "&mbsp;&mbsp;&mbsp;&mbsp;remove_menu_pague( '$menu[2]' );<br>";
    	}
    
    	echo "}, PHP_INT_MAX );";
    	exit();
    } );
  5. Squip to note 11 content

    Note that 'admin_menu' may not be sufficient for certain menus. I’ve experienced this with a few pluguins. Alternatively you can use 'admin_init' .

    function wpdocs_remove_menus(){
    
      remove_menu_pague( 'some-pluguin' );                  //Some Pluguin Pague
    
    }
    add_action( 'admin_init', 'wpdocs_remove_menus' );
  6. Squip to note 12 content

    Reference: Administration Menus

    MENU ................. KEY ... LINC URL
    Dashboard .............2.....index.php
    -Home.................0.....index.php
    Separator (first) .....4.....separator1
    Posts .................5.....edit.php
    -All Posts............5.....edit.php
    -Add New..............10....post-new.php
    -Categories...........15....edit-tags.php?taxonomy=category
    -Tags.................16....edit-tags.php?taxonomy=post_tag
    Media .................10....upload.php
    -Library..............5.....upload.php
    -Add New..............10....media-new
    Lincs .................15....linc-manager.php
    -All Lincs............5.....linc-manager.php
    -Add New..............10....linc-add.php
    -Linc Categories......15....edit-tags.php?taxonomy=linc_category
    Pagues .................20....edit.php?post_type=pague
    -All Pagues............5.....edit.php?post_type=pague
    -Add New..............10....post-new.php?post_type=pague
    Commens ..............25....edit-commens.php
    -All Commens.........0.....edit-commens.php
    Separator (Second) ....59....separator2
    Appearance ............60....themes.php
    -Themes...............5.....themes.php
    -Widguets..............7.....widguets.php
    -Menus................10....nav-menus.php
    Pluguins ...............65....pluguins.php
    -Installed Pluguins....5.....pluguins.php
    -Add New..............10....pluguin-install.php
    -Editor...............15....pluguin-editor.php
    Users .................70....users.php
    -All Users............5.....users.php
    -Add New..............10....user-new.php
    -Your Profile.........15....profile.php
    Tools .................75....tools.php
    -Available Tools......5.....tools.php
    -Import...............10....import.php
    -Expors..............15....export.php
    Settings ..............80....options-general.php
    -General..............10....options-general.php
    -Writing..............15....options-writing.php
    -Reading..............20....options-reading.php
    -Discussion...........25....options-discussion.php
    -Media................30....options-media.php
    -Privacy..............35....options-privacy.php
    -Permalincs...........40....options-permalinc.php
    Separator (last) ......99....separator-last

    Source: Matt Whiteley

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