5 * This code handles the building and rendering of the press bar.
9 * Instantiate the admin bar object and set it up as a global for access elsewhere.
11 * UNHOOKING THIS FUNCTION WILL NOT PROPERLY REMOVE THE ADMIN BAR.
12 * For that, use show_admin_bar(false) or the 'show_admin_bar' filter.
16 * @return bool Whether the admin bar was successfully initialized.
18 function _wp_admin_bar_init() {
21 if ( ! is_admin_bar_showing() )
24 /* Load the admin bar class code ready for instantiation */
25 require( ABSPATH . WPINC . '/class-wp-admin-bar.php' );
27 /* Instantiate the admin bar */
30 * Filter the admin bar class to instantiate.
34 * @param string $wp_admin_bar_class Admin bar class to use. Default 'WP_Admin_Bar'.
36 $admin_bar_class = apply_filters( 'wp_admin_bar_class', 'WP_Admin_Bar' );
37 if ( class_exists( $admin_bar_class ) )
38 $wp_admin_bar = new $admin_bar_class;
42 $wp_admin_bar->initialize();
43 $wp_admin_bar->add_menus();
47 // Don't remove. Wrong way to disable.
48 add_action( 'template_redirect', '_wp_admin_bar_init', 0 );
49 add_action( 'admin_init', '_wp_admin_bar_init' );
52 * Render the admin bar to the page based on the $wp_admin_bar->menu member var.
53 * This is called very late on the footer actions so that it will render after anything else being
54 * added to the footer.
56 * It includes the action "admin_bar_menu" which should be used to hook in and
57 * add new menus to the admin bar. That way you can be sure that you are adding at most optimal point,
58 * right before the admin bar is rendered. This also gives you access to the $post global, among others.
62 function wp_admin_bar_render() {
65 if ( ! is_admin_bar_showing() || ! is_object( $wp_admin_bar ) )
69 * Load all necessary admin bar items.
71 * This is the hook used to add, remove, or manipulate admin bar items.
75 * @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance, passed by reference
77 do_action_ref_array( 'admin_bar_menu', array( &$wp_admin_bar ) );
80 * Fires before the admin bar is rendered.
84 do_action( 'wp_before_admin_bar_render' );
86 $wp_admin_bar->render();
89 * Fires after the admin bar is rendered.
93 do_action( 'wp_after_admin_bar_render' );
95 add_action( 'wp_footer', 'wp_admin_bar_render', 1000 );
96 add_action( 'in_admin_header', 'wp_admin_bar_render', 0 );
99 * Add the WordPress logo menu.
103 * @param WP_Admin_Bar $wp_admin_bar
105 function wp_admin_bar_wp_menu( $wp_admin_bar ) {
106 $wp_admin_bar->add_menu( array(
108 'title' => '<span class="ab-icon"></span>',
109 'href' => self_admin_url( 'about.php' ),
111 'title' => __('About WordPress'),
115 if ( is_user_logged_in() ) {
116 // Add "About WordPress" link
117 $wp_admin_bar->add_menu( array(
118 'parent' => 'wp-logo',
120 'title' => __('About WordPress'),
121 'href' => self_admin_url( 'about.php' ),
125 // Add WordPress.org link
126 $wp_admin_bar->add_menu( array(
127 'parent' => 'wp-logo-external',
129 'title' => __('WordPress.org'),
130 'href' => __('http://wordpress.org/'),
134 $wp_admin_bar->add_menu( array(
135 'parent' => 'wp-logo-external',
136 'id' => 'documentation',
137 'title' => __('Documentation'),
138 'href' => __('http://codex.wordpress.org/'),
142 $wp_admin_bar->add_menu( array(
143 'parent' => 'wp-logo-external',
144 'id' => 'support-forums',
145 'title' => __('Support Forums'),
146 'href' => __('http://wordpress.org/support/'),
150 $wp_admin_bar->add_menu( array(
151 'parent' => 'wp-logo-external',
153 'title' => __('Feedback'),
154 'href' => __('http://wordpress.org/support/forum/requests-and-feedback'),
159 * Add the sidebar toggle button.
163 * @param WP_Admin_Bar $wp_admin_bar
165 function wp_admin_bar_sidebar_toggle( $wp_admin_bar ) {
167 $wp_admin_bar->add_menu( array(
168 'id' => 'menu-toggle',
169 'title' => '<span class="ab-icon"></span>',
172 'title' => __( 'Menu' ),
179 * Add the "My Account" item.
183 * @param WP_Admin_Bar $wp_admin_bar
185 function wp_admin_bar_my_account_item( $wp_admin_bar ) {
186 $user_id = get_current_user_id();
187 $current_user = wp_get_current_user();
188 $profile_url = get_edit_profile_url( $user_id );
193 $avatar = get_avatar( $user_id, 26 );
194 $howdy = sprintf( __('Howdy, %1$s'), $current_user->display_name );
195 $class = empty( $avatar ) ? '' : 'with-avatar';
197 $wp_admin_bar->add_menu( array(
198 'id' => 'my-account',
199 'parent' => 'top-secondary',
200 'title' => $howdy . $avatar,
201 'href' => $profile_url,
204 'title' => __('My Account'),
210 * Add the "My Account" submenu items.
214 * @param WP_Admin_Bar $wp_admin_bar
216 function wp_admin_bar_my_account_menu( $wp_admin_bar ) {
217 $user_id = get_current_user_id();
218 $current_user = wp_get_current_user();
219 $profile_url = get_edit_profile_url( $user_id );
224 $wp_admin_bar->add_group( array(
225 'parent' => 'my-account',
226 'id' => 'user-actions',
229 $user_info = get_avatar( $user_id, 64 );
230 $user_info .= "<span class='display-name'>{$current_user->display_name}</span>";
232 if ( $current_user->display_name !== $current_user->user_login )
233 $user_info .= "<span class='username'>{$current_user->user_login}</span>";
235 $wp_admin_bar->add_menu( array(
236 'parent' => 'user-actions',
238 'title' => $user_info,
239 'href' => $profile_url,
244 $wp_admin_bar->add_menu( array(
245 'parent' => 'user-actions',
246 'id' => 'edit-profile',
247 'title' => __( 'Edit My Profile' ),
248 'href' => $profile_url,
250 $wp_admin_bar->add_menu( array(
251 'parent' => 'user-actions',
253 'title' => __( 'Log Out' ),
254 'href' => wp_logout_url(),
259 * Add the "Site Name" menu.
263 * @param WP_Admin_Bar $wp_admin_bar
265 function wp_admin_bar_site_menu( $wp_admin_bar ) {
266 // Don't show for logged out users.
267 if ( ! is_user_logged_in() )
270 // Show only when the user is a member of this site, or they're a super admin.
271 if ( ! is_user_member_of_blog() && ! is_super_admin() )
274 $blogname = get_bloginfo('name');
276 if ( empty( $blogname ) )
277 $blogname = preg_replace( '#^(https?://)?(www.)?#', '', get_home_url() );
279 if ( is_network_admin() ) {
280 $blogname = sprintf( __('Network Admin: %s'), esc_html( get_current_site()->site_name ) );
281 } elseif ( is_user_admin() ) {
282 $blogname = sprintf( __('Global Dashboard: %s'), esc_html( get_current_site()->site_name ) );
285 $title = wp_html_excerpt( $blogname, 40, '…' );
287 $wp_admin_bar->add_menu( array(
290 'href' => is_admin() ? home_url( '/' ) : admin_url(),
293 // Create submenu items.
296 // Add an option to visit the site.
297 $wp_admin_bar->add_menu( array(
298 'parent' => 'site-name',
300 'title' => __( 'Visit Site' ),
301 'href' => home_url( '/' ),
304 if ( is_blog_admin() && is_multisite() && current_user_can( 'manage_sites' ) ) {
305 $wp_admin_bar->add_menu( array(
306 'parent' => 'site-name',
308 'title' => __( 'Edit Site' ),
309 'href' => network_admin_url( 'site-info.php?id=' . get_current_blog_id() ),
314 // We're on the front end, link to the Dashboard.
315 $wp_admin_bar->add_menu( array(
316 'parent' => 'site-name',
318 'title' => __( 'Dashboard' ),
319 'href' => admin_url(),
322 // Add the appearance submenu items.
323 wp_admin_bar_appearance_menu( $wp_admin_bar );
328 * Add the "My Sites/[Site Name]" menu and all submenus.
332 * @param WP_Admin_Bar $wp_admin_bar
334 function wp_admin_bar_my_sites_menu( $wp_admin_bar ) {
335 // Don't show for logged out users or single site mode.
336 if ( ! is_user_logged_in() || ! is_multisite() )
339 // Show only when the user has at least one site, or they're a super admin.
340 if ( count( $wp_admin_bar->user->blogs ) < 1 && ! is_super_admin() )
343 $wp_admin_bar->add_menu( array(
345 'title' => __( 'My Sites' ),
346 'href' => admin_url( 'my-sites.php' ),
349 if ( is_super_admin() ) {
350 $wp_admin_bar->add_group( array(
351 'parent' => 'my-sites',
352 'id' => 'my-sites-super-admin',
355 $wp_admin_bar->add_menu( array(
356 'parent' => 'my-sites-super-admin',
357 'id' => 'network-admin',
358 'title' => __('Network Admin'),
359 'href' => network_admin_url(),
362 $wp_admin_bar->add_menu( array(
363 'parent' => 'network-admin',
364 'id' => 'network-admin-d',
365 'title' => __( 'Dashboard' ),
366 'href' => network_admin_url(),
368 $wp_admin_bar->add_menu( array(
369 'parent' => 'network-admin',
370 'id' => 'network-admin-s',
371 'title' => __( 'Sites' ),
372 'href' => network_admin_url( 'sites.php' ),
374 $wp_admin_bar->add_menu( array(
375 'parent' => 'network-admin',
376 'id' => 'network-admin-u',
377 'title' => __( 'Users' ),
378 'href' => network_admin_url( 'users.php' ),
380 $wp_admin_bar->add_menu( array(
381 'parent' => 'network-admin',
382 'id' => 'network-admin-t',
383 'title' => __( 'Themes' ),
384 'href' => network_admin_url( 'themes.php' ),
386 $wp_admin_bar->add_menu( array(
387 'parent' => 'network-admin',
388 'id' => 'network-admin-p',
389 'title' => __( 'Plugins' ),
390 'href' => network_admin_url( 'plugins.php' ),
395 $wp_admin_bar->add_group( array(
396 'parent' => 'my-sites',
397 'id' => 'my-sites-list',
399 'class' => is_super_admin() ? 'ab-sub-secondary' : '',
403 foreach ( (array) $wp_admin_bar->user->blogs as $blog ) {
404 switch_to_blog( $blog->userblog_id );
406 $blavatar = '<div class="blavatar"></div>';
408 $blogname = empty( $blog->blogname ) ? $blog->domain : $blog->blogname;
409 $menu_id = 'blog-' . $blog->userblog_id;
411 $wp_admin_bar->add_menu( array(
412 'parent' => 'my-sites-list',
414 'title' => $blavatar . $blogname,
415 'href' => admin_url(),
418 $wp_admin_bar->add_menu( array(
419 'parent' => $menu_id,
420 'id' => $menu_id . '-d',
421 'title' => __( 'Dashboard' ),
422 'href' => admin_url(),
425 if ( current_user_can( get_post_type_object( 'post' )->cap->create_posts ) ) {
426 $wp_admin_bar->add_menu( array(
427 'parent' => $menu_id,
428 'id' => $menu_id . '-n',
429 'title' => __( 'New Post' ),
430 'href' => admin_url( 'post-new.php' ),
434 if ( current_user_can( 'edit_posts' ) ) {
435 $wp_admin_bar->add_menu( array(
436 'parent' => $menu_id,
437 'id' => $menu_id . '-c',
438 'title' => __( 'Manage Comments' ),
439 'href' => admin_url( 'edit-comments.php' ),
443 $wp_admin_bar->add_menu( array(
444 'parent' => $menu_id,
445 'id' => $menu_id . '-v',
446 'title' => __( 'Visit Site' ),
447 'href' => home_url( '/' ),
450 restore_current_blog();
455 * Provide a shortlink.
459 * @param WP_Admin_Bar $wp_admin_bar
461 function wp_admin_bar_shortlink_menu( $wp_admin_bar ) {
462 $short = wp_get_shortlink( 0, 'query' );
463 $id = 'get-shortlink';
465 if ( empty( $short ) )
468 $html = '<input class="shortlink-input" type="text" readonly="readonly" value="' . esc_attr( $short ) . '" />';
470 $wp_admin_bar->add_menu( array(
472 'title' => __( 'Shortlink' ),
474 'meta' => array( 'html' => $html ),
479 * Provide an edit link for posts and terms.
483 * @param WP_Admin_Bar $wp_admin_bar
485 function wp_admin_bar_edit_menu( $wp_admin_bar ) {
486 global $tag, $wp_the_query;
489 $current_screen = get_current_screen();
492 if ( 'post' == $current_screen->base
493 && 'add' != $current_screen->action
494 && ( $post_type_object = get_post_type_object( $post->post_type ) )
495 && current_user_can( 'read_post', $post->ID )
496 && ( $post_type_object->public )
497 && ( $post_type_object->show_in_admin_bar ) )
499 $wp_admin_bar->add_menu( array(
501 'title' => $post_type_object->labels->view_item,
502 'href' => get_permalink( $post->ID )
504 } elseif ( 'edit-tags' == $current_screen->base
505 && isset( $tag ) && is_object( $tag )
506 && ( $tax = get_taxonomy( $tag->taxonomy ) )
509 $wp_admin_bar->add_menu( array(
511 'title' => $tax->labels->view_item,
512 'href' => get_term_link( $tag )
516 $current_object = $wp_the_query->get_queried_object();
518 if ( empty( $current_object ) )
521 if ( ! empty( $current_object->post_type )
522 && ( $post_type_object = get_post_type_object( $current_object->post_type ) )
523 && current_user_can( 'edit_post', $current_object->ID )
524 && $post_type_object->show_ui && $post_type_object->show_in_admin_bar )
526 $wp_admin_bar->add_menu( array(
528 'title' => $post_type_object->labels->edit_item,
529 'href' => get_edit_post_link( $current_object->ID )
531 } elseif ( ! empty( $current_object->taxonomy )
532 && ( $tax = get_taxonomy( $current_object->taxonomy ) )
533 && current_user_can( $tax->cap->edit_terms )
536 $wp_admin_bar->add_menu( array(
538 'title' => $tax->labels->edit_item,
539 'href' => get_edit_term_link( $current_object->term_id, $current_object->taxonomy )
546 * Add "Add New" menu.
550 * @param WP_Admin_Bar $wp_admin_bar
552 function wp_admin_bar_new_content_menu( $wp_admin_bar ) {
555 $cpts = (array) get_post_types( array( 'show_in_admin_bar' => true ), 'objects' );
557 if ( isset( $cpts['post'] ) && current_user_can( $cpts['post']->cap->create_posts ) )
558 $actions[ 'post-new.php' ] = array( $cpts['post']->labels->name_admin_bar, 'new-post' );
560 if ( isset( $cpts['attachment'] ) && current_user_can( 'upload_files' ) )
561 $actions[ 'media-new.php' ] = array( $cpts['attachment']->labels->name_admin_bar, 'new-media' );
563 if ( current_user_can( 'manage_links' ) )
564 $actions[ 'link-add.php' ] = array( _x( 'Link', 'add new from admin bar' ), 'new-link' );
566 if ( isset( $cpts['page'] ) && current_user_can( $cpts['page']->cap->create_posts ) )
567 $actions[ 'post-new.php?post_type=page' ] = array( $cpts['page']->labels->name_admin_bar, 'new-page' );
569 unset( $cpts['post'], $cpts['page'], $cpts['attachment'] );
571 // Add any additional custom post types.
572 foreach ( $cpts as $cpt ) {
573 if ( ! current_user_can( $cpt->cap->create_posts ) )
576 $key = 'post-new.php?post_type=' . $cpt->name;
577 $actions[ $key ] = array( $cpt->labels->name_admin_bar, 'new-' . $cpt->name );
579 // Avoid clash with parent node and a 'content' post type.
580 if ( isset( $actions['post-new.php?post_type=content'] ) )
581 $actions['post-new.php?post_type=content'][1] = 'add-new-content';
583 if ( current_user_can( 'create_users' ) || current_user_can( 'promote_users' ) )
584 $actions[ 'user-new.php' ] = array( _x( 'User', 'add new from admin bar' ), 'new-user' );
589 $title = '<span class="ab-icon"></span><span class="ab-label">' . _x( 'New', 'admin bar menu group label' ) . '</span>';
591 $wp_admin_bar->add_menu( array(
592 'id' => 'new-content',
594 'href' => admin_url( current( array_keys( $actions ) ) ),
596 'title' => _x( 'Add New', 'admin bar menu group label' ),
600 foreach ( $actions as $link => $action ) {
601 list( $title, $id ) = $action;
603 $wp_admin_bar->add_menu( array(
604 'parent' => 'new-content',
607 'href' => admin_url( $link )
613 * Add edit comments link with awaiting moderation count bubble.
617 * @param WP_Admin_Bar $wp_admin_bar
619 function wp_admin_bar_comments_menu( $wp_admin_bar ) {
620 if ( !current_user_can('edit_posts') )
623 $awaiting_mod = wp_count_comments();
624 $awaiting_mod = $awaiting_mod->moderated;
625 $awaiting_title = esc_attr( sprintf( _n( '%s comment awaiting moderation', '%s comments awaiting moderation', $awaiting_mod ), number_format_i18n( $awaiting_mod ) ) );
627 $icon = '<span class="ab-icon"></span>';
628 $title = '<span id="ab-awaiting-mod" class="ab-label awaiting-mod pending-count count-' . $awaiting_mod . '">' . number_format_i18n( $awaiting_mod ) . '</span>';
630 $wp_admin_bar->add_menu( array(
632 'title' => $icon . $title,
633 'href' => admin_url('edit-comments.php'),
634 'meta' => array( 'title' => $awaiting_title ),
639 * Add appearance submenu items to the "Site Name" menu.
643 * @param WP_Admin_Bar $wp_admin_bar
645 function wp_admin_bar_appearance_menu( $wp_admin_bar ) {
646 $wp_admin_bar->add_group( array( 'parent' => 'site-name', 'id' => 'appearance' ) );
648 if ( current_user_can( 'switch_themes' ) || current_user_can( 'edit_theme_options' ) )
649 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'themes', 'title' => __('Themes'), 'href' => admin_url('themes.php') ) );
651 if ( ! current_user_can( 'edit_theme_options' ) )
654 $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
655 $wp_admin_bar->add_menu( array(
656 'parent' => 'appearance',
658 'title' => __('Customize'),
659 'href' => add_query_arg( 'url', urlencode( $current_url ), wp_customize_url() ),
661 'class' => 'hide-if-no-customize',
664 add_action( 'wp_before_admin_bar_render', 'wp_customize_support_script' );
666 if ( current_theme_supports( 'widgets' ) )
667 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'widgets', 'title' => __('Widgets'), 'href' => admin_url('widgets.php') ) );
669 if ( current_theme_supports( 'menus' ) || current_theme_supports( 'widgets' ) )
670 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'menus', 'title' => __('Menus'), 'href' => admin_url('nav-menus.php') ) );
672 if ( current_theme_supports( 'custom-background' ) )
673 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'background', 'title' => __('Background'), 'href' => admin_url('themes.php?page=custom-background') ) );
675 if ( current_theme_supports( 'custom-header' ) )
676 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'header', 'title' => __('Header'), 'href' => admin_url('themes.php?page=custom-header') ) );
680 * Provide an update link if theme/plugin/core updates are available.
684 * @param WP_Admin_Bar $wp_admin_bar
686 function wp_admin_bar_updates_menu( $wp_admin_bar ) {
688 $update_data = wp_get_update_data();
690 if ( !$update_data['counts']['total'] )
693 $title = '<span class="ab-icon"></span><span class="ab-label">' . number_format_i18n( $update_data['counts']['total'] ) . '</span>';
694 $title .= '<span class="screen-reader-text">' . $update_data['title'] . '</span>';
696 $wp_admin_bar->add_menu( array(
699 'href' => network_admin_url( 'update-core.php' ),
701 'title' => $update_data['title'],
711 * @param WP_Admin_Bar $wp_admin_bar
713 function wp_admin_bar_search_menu( $wp_admin_bar ) {
717 $form = '<form action="' . esc_url( home_url( '/' ) ) . '" method="get" id="adminbarsearch">';
718 $form .= '<input class="adminbar-input" name="s" id="adminbar-search" type="text" value="" maxlength="150" />';
719 $form .= '<input type="submit" class="adminbar-button" value="' . __('Search') . '"/>';
722 $wp_admin_bar->add_menu( array(
723 'parent' => 'top-secondary',
727 'class' => 'admin-bar-search',
734 * Add secondary menus.
738 * @param WP_Admin_Bar $wp_admin_bar
740 function wp_admin_bar_add_secondary_groups( $wp_admin_bar ) {
741 $wp_admin_bar->add_group( array(
742 'id' => 'top-secondary',
744 'class' => 'ab-top-secondary',
748 $wp_admin_bar->add_group( array(
749 'parent' => 'wp-logo',
750 'id' => 'wp-logo-external',
752 'class' => 'ab-sub-secondary',
758 * Style and scripts for the admin bar.
762 function wp_admin_bar_header() { ?>
763 <style type="text/css" media="print">#wpadminbar { display:none; }</style>
768 * Default admin bar callback.
772 function _admin_bar_bump_cb() { ?>
773 <style type="text/css" media="screen">
774 html { margin-top: 32px !important; }
775 * html body { margin-top: 32px !important; }
776 @media screen and ( max-width: 782px ) {
777 html { margin-top: 46px !important; }
778 * html body { margin-top: 46px !important; }
785 * Set the display status of the admin bar.
787 * This can be called immediately upon plugin load. It does not need to be called from a function hooked to the init action.
791 * @param bool $show Whether to allow the admin bar to show.
794 function show_admin_bar( $show ) {
795 global $show_admin_bar;
796 $show_admin_bar = (bool) $show;
800 * Determine whether the admin bar should be showing.
804 * @return bool Whether the admin bar should be showing.
806 function is_admin_bar_showing() {
807 global $show_admin_bar, $pagenow;
809 // For all these types of requests, we never want an admin bar.
810 if ( defined('XMLRPC_REQUEST') || defined('DOING_AJAX') || defined('IFRAME_REQUEST') )
813 // Integrated into the admin.
817 if ( ! isset( $show_admin_bar ) ) {
818 if ( ! is_user_logged_in() || 'wp-login.php' == $pagenow ) {
819 $show_admin_bar = false;
821 $show_admin_bar = _get_admin_bar_pref();
826 * Filter whether to show the admin bar.
828 * Returning false to this hook is the recommended way to hide the admin bar.
829 * The user's display preference is used for logged in users.
833 * @param bool $show_admin_bar Whether the admin bar should be shown. Default false.
835 $show_admin_bar = apply_filters( 'show_admin_bar', $show_admin_bar );
837 return $show_admin_bar;
841 * Retrieve the admin bar display preference of a user.
846 * @param string $context Context of this preference check. Defaults to 'front'. The 'admin'
847 * preference is no longer used.
848 * @param int $user Optional. ID of the user to check, defaults to 0 for current user.
849 * @return bool Whether the admin bar should be showing for this user.
851 function _get_admin_bar_pref( $context = 'front', $user = 0 ) {
852 $pref = get_user_option( "show_admin_bar_{$context}", $user );
853 if ( false === $pref )
856 return 'true' === $pref;