remove_menu_page()移除后台管理界面的菜单

目录

描述

Remove a top level admin menu.

Please be aware that this would not prevent a user from accessing these screens directly. Removing a menu does not replace the need to filter a user's permissions as appropriate.

remove_menu_page() 描述

用法

This function should be called on the admin_menu action hook. Calling it elsewhere could cause issues : either the function is not defined or the global $menu variable used but this function is not yet declared.<?php 
function custom_menu_page_removing() {
    
remove_menu_page$menu_slug );
}
add_action'admin_menu''custom_menu_page_removing' );
?>

remove_menu_page() 用法

参数

$menu_slug

(string) (必填) The slug of the menu (typically the name of the PHP script for the built in menu items; example: edit-comments.php)

默认值: None

remove_menu_page() 参数

返回值

(array|boolean) 

The removed menu on success, false if not found.

remove_menu_page() 返回值

示例

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

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<?php
function remove_menus(){
  
  remove_menu_page( 'index.php' );                  //Dashboard
  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', 'remove_menus' );
?>

remove_menu_page() 示例

注意

  • 使用到 global: (unknown type) $menu
  • To remove submenu items in the admin, use remove_submenu_page. Using remove_menu_page() will not work for submenu items.

remove_menu_page() 注意

历史

添加于 版本: 3.1.0

remove_menu_page() 历史

源文件

remove_menu_page() 函数的代码位于 /wp-admin/includes/plugin.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Remove a top level admin menu
 *
 * @since 3.1.0
 *
 * @global array $menu
 *
 * @param string $menu_slug The slug of the menu
 * @return array|bool The removed menu on success, False if not found
 */
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;
}

remove_menu_page() 源文件

相关