wp_admin_bar_my_sites_menu

Add the "My Sites/[Site Name]" menu and all submenus.

Description

wp_admin_bar_my_sites_menu( (WP_Admin_Bar) $wp_admin_bar ); 

Parameters (1)

0. $wp_admin_bar (WP_Admin_Bar)
The wp admin bar.

Usage

  1. if ( !function_exists( 'wp_admin_bar_my_sites_menu' ) ) { 
  2. require_once ABSPATH . WPINC . '/admin-bar.php'; 
  3.  
  4. // The wp admin bar. 
  5. $wp_admin_bar = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wp_admin_bar_my_sites_menu($wp_admin_bar); 
  9.  

Defined (1)

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

/wp-includes/admin-bar.php  
  1. function wp_admin_bar_my_sites_menu( $wp_admin_bar ) { 
  2. // Don't show for logged out users or single site mode. 
  3. if ( ! is_user_logged_in() || ! is_multisite() ) 
  4. return; 
  5.  
  6. // Show only when the user has at least one site, or they're a super admin. 
  7. if ( count( $wp_admin_bar->user->blogs ) < 1 && ! is_super_admin() ) 
  8. return; 
  9.  
  10. if ( $wp_admin_bar->user->active_blog ) { 
  11. $my_sites_url = get_admin_url( $wp_admin_bar->user->active_blog->blog_id, 'my-sites.php' ); 
  12. } else { 
  13. $my_sites_url = admin_url( 'my-sites.php' ); 
  14.  
  15. $wp_admin_bar->add_menu( array( 
  16. 'id' => 'my-sites',  
  17. 'title' => __( 'My Sites' ),  
  18. 'href' => $my_sites_url,  
  19. ) ); 
  20.  
  21. if ( is_super_admin() ) { 
  22. $wp_admin_bar->add_group( array( 
  23. 'parent' => 'my-sites',  
  24. 'id' => 'my-sites-super-admin',  
  25. ) ); 
  26.  
  27. $wp_admin_bar->add_menu( array( 
  28. 'parent' => 'my-sites-super-admin',  
  29. 'id' => 'network-admin',  
  30. 'title' => __('Network Admin'),  
  31. 'href' => network_admin_url(),  
  32. ) ); 
  33.  
  34. $wp_admin_bar->add_menu( array( 
  35. 'parent' => 'network-admin',  
  36. 'id' => 'network-admin-d',  
  37. 'title' => __( 'Dashboard' ),  
  38. 'href' => network_admin_url(),  
  39. ) ); 
  40. $wp_admin_bar->add_menu( array( 
  41. 'parent' => 'network-admin',  
  42. 'id' => 'network-admin-s',  
  43. 'title' => __( 'Sites' ),  
  44. 'href' => network_admin_url( 'sites.php' ),  
  45. ) ); 
  46. $wp_admin_bar->add_menu( array( 
  47. 'parent' => 'network-admin',  
  48. 'id' => 'network-admin-u',  
  49. 'title' => __( 'Users' ),  
  50. 'href' => network_admin_url( 'users.php' ),  
  51. ) ); 
  52. $wp_admin_bar->add_menu( array( 
  53. 'parent' => 'network-admin',  
  54. 'id' => 'network-admin-t',  
  55. 'title' => __( 'Themes' ),  
  56. 'href' => network_admin_url( 'themes.php' ),  
  57. ) ); 
  58. $wp_admin_bar->add_menu( array( 
  59. 'parent' => 'network-admin',  
  60. 'id' => 'network-admin-p',  
  61. 'title' => __( 'Plugins' ),  
  62. 'href' => network_admin_url( 'plugins.php' ),  
  63. ) ); 
  64. $wp_admin_bar->add_menu( array( 
  65. 'parent' => 'network-admin',  
  66. 'id' => 'network-admin-o',  
  67. 'title' => __( 'Settings' ),  
  68. 'href' => network_admin_url( 'settings.php' ),  
  69. ) ); 
  70.  
  71. // Add site links 
  72. $wp_admin_bar->add_group( array( 
  73. 'parent' => 'my-sites',  
  74. 'id' => 'my-sites-list',  
  75. 'meta' => array( 
  76. 'class' => is_super_admin() ? 'ab-sub-secondary' : '',  
  77. ),  
  78. ) ); 
  79.  
  80. foreach ( (array) $wp_admin_bar->user->blogs as $blog ) { 
  81. switch_to_blog( $blog->userblog_id ); 
  82.  
  83. $blavatar = '<div class="blavatar"></div>'; 
  84.  
  85. $blogname = $blog->blogname; 
  86.  
  87. if ( ! $blogname ) { 
  88. $blogname = preg_replace( '#^(https?://)?(www.)?#', '', get_home_url() ); 
  89.  
  90. $menu_id = 'blog-' . $blog->userblog_id; 
  91.  
  92. $wp_admin_bar->add_menu( array( 
  93. 'parent' => 'my-sites-list',  
  94. 'id' => $menu_id,  
  95. 'title' => $blavatar . $blogname,  
  96. 'href' => admin_url(),  
  97. ) ); 
  98.  
  99. $wp_admin_bar->add_menu( array( 
  100. 'parent' => $menu_id,  
  101. 'id' => $menu_id . '-d',  
  102. 'title' => __( 'Dashboard' ),  
  103. 'href' => admin_url(),  
  104. ) ); 
  105.  
  106. if ( current_user_can( get_post_type_object( 'post' )->cap->create_posts ) ) { 
  107. $wp_admin_bar->add_menu( array( 
  108. 'parent' => $menu_id,  
  109. 'id' => $menu_id . '-n',  
  110. 'title' => __( 'New Post' ),  
  111. 'href' => admin_url( 'post-new.php' ),  
  112. ) ); 
  113.  
  114. if ( current_user_can( 'edit_posts' ) ) { 
  115. $wp_admin_bar->add_menu( array( 
  116. 'parent' => $menu_id,  
  117. 'id' => $menu_id . '-c',  
  118. 'title' => __( 'Manage Comments' ),  
  119. 'href' => admin_url( 'edit-comments.php' ),  
  120. ) ); 
  121.  
  122. $wp_admin_bar->add_menu( array( 
  123. 'parent' => $menu_id,  
  124. 'id' => $menu_id . '-v',  
  125. 'title' => __( 'Visit Site' ),  
  126. 'href' => home_url( '/' ),  
  127. ) ); 
  128.