_wp_menu_output

Display menu.

Description

_wp_menu_output( (array) $menu, (array) $submenu, (bool) $submenu_as_parent = true ); 

Parameters (3)

0. $menu (array)
The menu.
1. $submenu (array)
The submenu.
2. $submenu_as_parent — Optional. (bool) => true
The submenu as parent.

Usage

  1. if ( !function_exists( '_wp_menu_output' ) ) { 
  2. require_once ABSPATH . '/wp-admin/menu-header.php'; 
  3.  
  4. // The menu. 
  5. $menu = array(); 
  6.  
  7. // The submenu. 
  8. $submenu = array(); 
  9.  
  10. // The submenu as parent. 
  11. $submenu_as_parent = true; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = _wp_menu_output($menu, $submenu, $submenu_as_parent); 
  15.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/menu-header.php  
  1. function _wp_menu_output( $menu, $submenu, $submenu_as_parent = true ) { 
  2. global $self, $parent_file, $submenu_file, $plugin_page, $typenow; 
  3.  
  4. $first = true; 
  5. // 0 = menu_title, 1 = capability, 2 = menu_slug, 3 = page_title, 4 = classes, 5 = hookname, 6 = icon_url 
  6. foreach ( $menu as $key => $item ) { 
  7. $admin_is_parent = false; 
  8. $class = array(); 
  9. $aria_attributes = ''; 
  10. $aria_hidden = ''; 
  11. $is_separator = false; 
  12.  
  13. if ( $first ) { 
  14. $class[] = 'wp-first-item'; 
  15. $first = false; 
  16.  
  17. $submenu_items = array(); 
  18. if ( ! empty( $submenu[$item[2]] ) ) { 
  19. $class[] = 'wp-has-submenu'; 
  20. $submenu_items = $submenu[$item[2]]; 
  21.  
  22. if ( ( $parent_file && $item[2] == $parent_file ) || ( empty($typenow) && $self == $item[2] ) ) { 
  23. $class[] = ! empty( $submenu_items ) ? 'wp-has-current-submenu wp-menu-open' : 'current'; 
  24. } else { 
  25. $class[] = 'wp-not-current-submenu'; 
  26. if ( ! empty( $submenu_items ) ) 
  27. $aria_attributes .= 'aria-haspopup="true"'; 
  28.  
  29. if ( ! empty( $item[4] ) ) 
  30. $class[] = esc_attr( $item[4] ); 
  31.  
  32. $class = $class ? ' class="' . join( ' ', $class ) . '"' : ''; 
  33. $id = ! empty( $item[5] ) ? ' id="' . preg_replace( '|[^a-zA-Z0-9_:.]|', '-', $item[5] ) . '"' : ''; 
  34. $img = $img_style = ''; 
  35. $img_class = ' dashicons-before'; 
  36.  
  37. if ( false !== strpos( $class, 'wp-menu-separator' ) ) { 
  38. $is_separator = true; 
  39.  
  40. /** 
  41. * If the string 'none' (previously 'div') is passed instead of a URL, don't output 
  42. * the default menu image so an icon can be added to div.wp-menu-image as background 
  43. * with CSS. Dashicons and base64-encoded data:image/svg_xml URIs are also handled 
  44. * as special cases. 
  45. */ 
  46. if ( ! empty( $item[6] ) ) { 
  47. $img = '<img src="' . $item[6] . '" alt="" />'; 
  48.  
  49. if ( 'none' === $item[6] || 'div' === $item[6] ) { 
  50. $img = '<br />'; 
  51. } elseif ( 0 === strpos( $item[6], 'data:image/svg+xml;base64, ' ) ) { 
  52. $img = '<br />'; 
  53. $img_style = ' style="background-image:url(\'' . esc_attr( $item[6] ) . '\')"'; 
  54. $img_class = ' svg'; 
  55. } elseif ( 0 === strpos( $item[6], 'dashicons-' ) ) { 
  56. $img = '<br />'; 
  57. $img_class = ' dashicons-before ' . sanitize_html_class( $item[6] ); 
  58. $arrow = '<div class="wp-menu-arrow"><div></div></div>'; 
  59.  
  60. $title = wptexturize( $item[0] ); 
  61.  
  62. // hide separators from screen readers 
  63. if ( $is_separator ) { 
  64. $aria_hidden = ' aria-hidden="true"'; 
  65.  
  66. echo "\n\t<li$class$id$aria_hidden>"; 
  67.  
  68. if ( $is_separator ) { 
  69. echo '<div class="separator"></div>'; 
  70. } elseif ( $submenu_as_parent && ! empty( $submenu_items ) ) { 
  71. $submenu_items = array_values( $submenu_items ); // Re-index. 
  72. $menu_hook = get_plugin_page_hook( $submenu_items[0][2], $item[2] ); 
  73. $menu_file = $submenu_items[0][2]; 
  74. if ( false !== ( $pos = strpos( $menu_file, '?' ) ) ) 
  75. $menu_file = substr( $menu_file, 0, $pos ); 
  76. if ( ! empty( $menu_hook ) || ( ( 'index.php' != $submenu_items[0][2] ) && file_exists( WP_PLUGIN_DIR . "/$menu_file" ) && ! file_exists( ABSPATH . "/wp-admin/$menu_file" ) ) ) { 
  77. $admin_is_parent = true; 
  78. echo "<a href='admin.php?page={$submenu_items[0][2]}'$class $aria_attributes>$arrow<div class='wp-menu-image$img_class'$img_style>$img</div><div class='wp-menu-name'>$title</div></a>"; 
  79. } else { 
  80. echo "\n\t<a href='{$submenu_items[0][2]}'$class $aria_attributes>$arrow<div class='wp-menu-image$img_class'$img_style>$img</div><div class='wp-menu-name'>$title</div></a>"; 
  81. } elseif ( ! empty( $item[2] ) && current_user_can( $item[1] ) ) { 
  82. $menu_hook = get_plugin_page_hook( $item[2], 'admin.php' ); 
  83. $menu_file = $item[2]; 
  84. if ( false !== ( $pos = strpos( $menu_file, '?' ) ) ) 
  85. $menu_file = substr( $menu_file, 0, $pos ); 
  86. if ( ! empty( $menu_hook ) || ( ( 'index.php' != $item[2] ) && file_exists( WP_PLUGIN_DIR . "/$menu_file" ) && ! file_exists( ABSPATH . "/wp-admin/$menu_file" ) ) ) { 
  87. $admin_is_parent = true; 
  88. echo "\n\t<a href='admin.php?page={$item[2]}'$class $aria_attributes>$arrow<div class='wp-menu-image$img_class'$img_style>$img</div><div class='wp-menu-name'>{$item[0]}</div></a>"; 
  89. } else { 
  90. echo "\n\t<a href='{$item[2]}'$class $aria_attributes>$arrow<div class='wp-menu-image$img_class'$img_style>$img</div><div class='wp-menu-name'>{$item[0]}</div></a>"; 
  91.  
  92. if ( ! empty( $submenu_items ) ) { 
  93. echo "\n\t<ul class='wp-submenu wp-submenu-wrap'>"; 
  94. echo "<li class='wp-submenu-head' aria-hidden='true'>{$item[0]}</li>"; 
  95.  
  96. $first = true; 
  97.  
  98. // 0 = menu_title, 1 = capability, 2 = menu_slug, 3 = page_title, 4 = classes 
  99. foreach ( $submenu_items as $sub_key => $sub_item ) { 
  100. if ( ! current_user_can( $sub_item[1] ) ) 
  101. continue; 
  102.  
  103. $class = array(); 
  104. if ( $first ) { 
  105. $class[] = 'wp-first-item'; 
  106. $first = false; 
  107.  
  108. $menu_file = $item[2]; 
  109.  
  110. if ( false !== ( $pos = strpos( $menu_file, '?' ) ) ) 
  111. $menu_file = substr( $menu_file, 0, $pos ); 
  112.  
  113. // Handle current for post_type=post|page|foo pages, which won't match $self. 
  114. $self_type = ! empty( $typenow ) ? $self . '?post_type=' . $typenow : 'nothing'; 
  115.  
  116. if ( isset( $submenu_file ) ) { 
  117. if ( $submenu_file == $sub_item[2] ) 
  118. $class[] = 'current'; 
  119. // If plugin_page is set the parent must either match the current page or not physically exist. 
  120. // This allows plugin pages with the same hook to exist under different parents. 
  121. } elseif ( 
  122. ( ! isset( $plugin_page ) && $self == $sub_item[2] ) || 
  123. ( isset( $plugin_page ) && $plugin_page == $sub_item[2] && ( $item[2] == $self_type || $item[2] == $self || file_exists($menu_file) === false ) ) 
  124. ) { 
  125. $class[] = 'current'; 
  126.  
  127. if ( ! empty( $sub_item[4] ) ) { 
  128. $class[] = esc_attr( $sub_item[4] ); 
  129.  
  130. $class = $class ? ' class="' . join( ' ', $class ) . '"' : ''; 
  131.  
  132. $menu_hook = get_plugin_page_hook($sub_item[2], $item[2]); 
  133. $sub_file = $sub_item[2]; 
  134. if ( false !== ( $pos = strpos( $sub_file, '?' ) ) ) 
  135. $sub_file = substr($sub_file, 0, $pos); 
  136.  
  137. $title = wptexturize($sub_item[0]); 
  138.  
  139. if ( ! empty( $menu_hook ) || ( ( 'index.php' != $sub_item[2] ) && file_exists( WP_PLUGIN_DIR . "/$sub_file" ) && ! file_exists( ABSPATH . "/wp-admin/$sub_file" ) ) ) { 
  140. // If admin.php is the current page or if the parent exists as a file in the plugins or admin dir 
  141. if ( ( ! $admin_is_parent && file_exists( WP_PLUGIN_DIR . "/$menu_file" ) && ! is_dir( WP_PLUGIN_DIR . "/{$item[2]}" ) ) || file_exists( $menu_file ) ) 
  142. $sub_item_url = add_query_arg( array( 'page' => $sub_item[2] ), $item[2] ); 
  143. else 
  144. $sub_item_url = add_query_arg( array( 'page' => $sub_item[2] ), 'admin.php' ); 
  145.  
  146. $sub_item_url = esc_url( $sub_item_url ); 
  147. echo "<li$class><a href='$sub_item_url'$class>$title</a></li>"; 
  148. } else { 
  149. echo "<li$class><a href='{$sub_item[2]}'$class>$title</a></li>"; 
  150. echo "</ul>"; 
  151. echo "</li>"; 
  152.  
  153. echo '<li id="collapse-menu" class="hide-if-no-js">' . 
  154. '<button type="button" id="collapse-button" aria-label="' . esc_attr__( 'Collapse Main menu' ) . '" aria-expanded="true">' . 
  155. '<span class="collapse-button-icon" aria-hidden="true"></span>' . 
  156. '<span class="collapse-button-label">' . __( 'Collapse menu' ) . '</span>' . 
  157. '</button></li>';