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 */
28 $admin_bar_class = apply_filters( 'wp_admin_bar_class', 'WP_Admin_Bar' );
29 if ( class_exists( $admin_bar_class ) )
30 $wp_admin_bar = new $admin_bar_class;
34 $wp_admin_bar->initialize();
35 $wp_admin_bar->add_menus();
39 // Don't remove. Wrong way to disable.
40 add_action( 'template_redirect', '_wp_admin_bar_init', 0 );
41 add_action( 'admin_init', '_wp_admin_bar_init' );
44 * Render the admin bar to the page based on the $wp_admin_bar->menu member var.
45 * This is called very late on the footer actions so that it will render after anything else being
46 * added to the footer.
48 * It includes the action "admin_bar_menu" which should be used to hook in and
49 * add new menus to the admin bar. That way you can be sure that you are adding at most optimal point,
50 * right before the admin bar is rendered. This also gives you access to the $post global, among others.
54 function wp_admin_bar_render() {
57 if ( ! is_admin_bar_showing() || ! is_object( $wp_admin_bar ) )
60 do_action_ref_array( 'admin_bar_menu', array( &$wp_admin_bar ) );
62 do_action( 'wp_before_admin_bar_render' );
64 $wp_admin_bar->render();
66 do_action( 'wp_after_admin_bar_render' );
68 add_action( 'wp_footer', 'wp_admin_bar_render', 1000 );
69 add_action( 'in_admin_header', 'wp_admin_bar_render', 0 );
72 * Add the WordPress logo menu.
76 * @param WP_Admin_Bar $wp_admin_bar
78 function wp_admin_bar_wp_menu( $wp_admin_bar ) {
79 $wp_admin_bar->add_menu( array(
81 'title' => '<span class="ab-icon"></span>',
82 'href' => self_admin_url( 'about.php' ),
84 'title' => __('About WordPress'),
88 if ( is_user_logged_in() ) {
89 // Add "About WordPress" link
90 $wp_admin_bar->add_menu( array(
91 'parent' => 'wp-logo',
93 'title' => __('About WordPress'),
94 'href' => self_admin_url( 'about.php' ),
98 // Add WordPress.org link
99 $wp_admin_bar->add_menu( array(
100 'parent' => 'wp-logo-external',
102 'title' => __('WordPress.org'),
103 'href' => __('http://wordpress.org/'),
107 $wp_admin_bar->add_menu( array(
108 'parent' => 'wp-logo-external',
109 'id' => 'documentation',
110 'title' => __('Documentation'),
111 'href' => __('http://codex.wordpress.org/'),
115 $wp_admin_bar->add_menu( array(
116 'parent' => 'wp-logo-external',
117 'id' => 'support-forums',
118 'title' => __('Support Forums'),
119 'href' => __('http://wordpress.org/support/'),
123 $wp_admin_bar->add_menu( array(
124 'parent' => 'wp-logo-external',
126 'title' => __('Feedback'),
127 'href' => __('http://wordpress.org/support/forum/requests-and-feedback'),
132 * Add the "My Account" item.
136 * @param WP_Admin_Bar $wp_admin_bar
138 function wp_admin_bar_my_account_item( $wp_admin_bar ) {
139 $user_id = get_current_user_id();
140 $current_user = wp_get_current_user();
141 $profile_url = get_edit_profile_url( $user_id );
146 $avatar = get_avatar( $user_id, 16 );
147 $howdy = sprintf( __('Howdy, %1$s'), $current_user->display_name );
148 $class = empty( $avatar ) ? '' : 'with-avatar';
150 $wp_admin_bar->add_menu( array(
151 'id' => 'my-account',
152 'parent' => 'top-secondary',
153 'title' => $howdy . $avatar,
154 'href' => $profile_url,
157 'title' => __('My Account'),
163 * Add the "My Account" submenu items.
167 * @param WP_Admin_Bar $wp_admin_bar
169 function wp_admin_bar_my_account_menu( $wp_admin_bar ) {
170 $user_id = get_current_user_id();
171 $current_user = wp_get_current_user();
172 $profile_url = get_edit_profile_url( $user_id );
177 $wp_admin_bar->add_group( array(
178 'parent' => 'my-account',
179 'id' => 'user-actions',
182 $user_info = get_avatar( $user_id, 64 );
183 $user_info .= "<span class='display-name'>{$current_user->display_name}</span>";
185 if ( $current_user->display_name !== $current_user->user_login )
186 $user_info .= "<span class='username'>{$current_user->user_login}</span>";
188 $wp_admin_bar->add_menu( array(
189 'parent' => 'user-actions',
191 'title' => $user_info,
192 'href' => $profile_url,
197 $wp_admin_bar->add_menu( array(
198 'parent' => 'user-actions',
199 'id' => 'edit-profile',
200 'title' => __( 'Edit My Profile' ),
201 'href' => $profile_url,
203 $wp_admin_bar->add_menu( array(
204 'parent' => 'user-actions',
206 'title' => __( 'Log Out' ),
207 'href' => wp_logout_url(),
212 * Add the "Site Name" menu.
216 * @param WP_Admin_Bar $wp_admin_bar
218 function wp_admin_bar_site_menu( $wp_admin_bar ) {
219 global $current_site;
221 // Don't show for logged out users.
222 if ( ! is_user_logged_in() )
225 // Show only when the user is a member of this site, or they're a super admin.
226 if ( ! is_user_member_of_blog() && ! is_super_admin() )
229 $blogname = get_bloginfo('name');
231 if ( empty( $blogname ) )
232 $blogname = preg_replace( '#^(https?://)?(www.)?#', '', get_home_url() );
234 if ( is_network_admin() ) {
235 $blogname = sprintf( __('Network Admin: %s'), esc_html( $current_site->site_name ) );
236 } elseif ( is_user_admin() ) {
237 $blogname = sprintf( __('Global Dashboard: %s'), esc_html( $current_site->site_name ) );
240 $title = wp_html_excerpt( $blogname, 40, '…' );
242 $wp_admin_bar->add_menu( array(
245 'href' => is_admin() ? home_url( '/' ) : admin_url(),
248 // Create submenu items.
251 // Add an option to visit the site.
252 $wp_admin_bar->add_menu( array(
253 'parent' => 'site-name',
255 'title' => __( 'Visit Site' ),
256 'href' => home_url( '/' ),
259 if ( is_blog_admin() && is_multisite() && current_user_can( 'manage_sites' ) ) {
260 $wp_admin_bar->add_menu( array(
261 'parent' => 'site-name',
263 'title' => __( 'Edit Site' ),
264 'href' => network_admin_url( 'site-info.php?id=' . get_current_blog_id() ),
269 // We're on the front end, link to the Dashboard.
270 $wp_admin_bar->add_menu( array(
271 'parent' => 'site-name',
273 'title' => __( 'Dashboard' ),
274 'href' => admin_url(),
277 // Add the appearance submenu items.
278 wp_admin_bar_appearance_menu( $wp_admin_bar );
283 * Add the "My Sites/[Site Name]" menu and all submenus.
287 * @param WP_Admin_Bar $wp_admin_bar
289 function wp_admin_bar_my_sites_menu( $wp_admin_bar ) {
290 // Don't show for logged out users or single site mode.
291 if ( ! is_user_logged_in() || ! is_multisite() )
294 // Show only when the user has at least one site, or they're a super admin.
295 if ( count( $wp_admin_bar->user->blogs ) < 1 && ! is_super_admin() )
298 $wp_admin_bar->add_menu( array(
300 'title' => __( 'My Sites' ),
301 'href' => admin_url( 'my-sites.php' ),
304 if ( is_super_admin() ) {
305 $wp_admin_bar->add_group( array(
306 'parent' => 'my-sites',
307 'id' => 'my-sites-super-admin',
310 $wp_admin_bar->add_menu( array(
311 'parent' => 'my-sites-super-admin',
312 'id' => 'network-admin',
313 'title' => __('Network Admin'),
314 'href' => network_admin_url(),
317 $wp_admin_bar->add_menu( array(
318 'parent' => 'network-admin',
319 'id' => 'network-admin-d',
320 'title' => __( 'Dashboard' ),
321 'href' => network_admin_url(),
323 $wp_admin_bar->add_menu( array(
324 'parent' => 'network-admin',
325 'id' => 'network-admin-s',
326 'title' => __( 'Sites' ),
327 'href' => network_admin_url( 'sites.php' ),
329 $wp_admin_bar->add_menu( array(
330 'parent' => 'network-admin',
331 'id' => 'network-admin-u',
332 'title' => __( 'Users' ),
333 'href' => network_admin_url( 'users.php' ),
335 $wp_admin_bar->add_menu( array(
336 'parent' => 'network-admin',
337 'id' => 'network-admin-t',
338 'title' => __( 'Themes' ),
339 'href' => network_admin_url( 'themes.php' ),
341 $wp_admin_bar->add_menu( array(
342 'parent' => 'network-admin',
343 'id' => 'network-admin-p',
344 'title' => __( 'Plugins' ),
345 'href' => network_admin_url( 'plugins.php' ),
350 $wp_admin_bar->add_group( array(
351 'parent' => 'my-sites',
352 'id' => 'my-sites-list',
354 'class' => is_super_admin() ? 'ab-sub-secondary' : '',
358 foreach ( (array) $wp_admin_bar->user->blogs as $blog ) {
359 switch_to_blog( $blog->userblog_id );
361 $blavatar = '<div class="blavatar"></div>';
363 $blogname = empty( $blog->blogname ) ? $blog->domain : $blog->blogname;
364 $menu_id = 'blog-' . $blog->userblog_id;
366 $wp_admin_bar->add_menu( array(
367 'parent' => 'my-sites-list',
369 'title' => $blavatar . $blogname,
370 'href' => admin_url(),
373 $wp_admin_bar->add_menu( array(
374 'parent' => $menu_id,
375 'id' => $menu_id . '-d',
376 'title' => __( 'Dashboard' ),
377 'href' => admin_url(),
380 if ( current_user_can( get_post_type_object( 'post' )->cap->create_posts ) ) {
381 $wp_admin_bar->add_menu( array(
382 'parent' => $menu_id,
383 'id' => $menu_id . '-n',
384 'title' => __( 'New Post' ),
385 'href' => admin_url( 'post-new.php' ),
389 if ( current_user_can( 'edit_posts' ) ) {
390 $wp_admin_bar->add_menu( array(
391 'parent' => $menu_id,
392 'id' => $menu_id . '-c',
393 'title' => __( 'Manage Comments' ),
394 'href' => admin_url( 'edit-comments.php' ),
398 $wp_admin_bar->add_menu( array(
399 'parent' => $menu_id,
400 'id' => $menu_id . '-v',
401 'title' => __( 'Visit Site' ),
402 'href' => home_url( '/' ),
405 restore_current_blog();
410 * Provide a shortlink.
414 * @param WP_Admin_Bar $wp_admin_bar
416 function wp_admin_bar_shortlink_menu( $wp_admin_bar ) {
417 $short = wp_get_shortlink( 0, 'query' );
418 $id = 'get-shortlink';
420 if ( empty( $short ) )
423 $html = '<input class="shortlink-input" type="text" readonly="readonly" value="' . esc_attr( $short ) . '" />';
425 $wp_admin_bar->add_menu( array(
427 'title' => __( 'Shortlink' ),
429 'meta' => array( 'html' => $html ),
434 * Provide an edit link for posts and terms.
438 * @param WP_Admin_Bar $wp_admin_bar
440 function wp_admin_bar_edit_menu( $wp_admin_bar ) {
441 global $tag, $wp_the_query;
444 $current_screen = get_current_screen();
447 if ( 'post' == $current_screen->base
448 && 'add' != $current_screen->action
449 && ( $post_type_object = get_post_type_object( $post->post_type ) )
450 && current_user_can( 'read_post', $post->ID )
451 && ( $post_type_object->public )
452 && ( $post_type_object->show_in_admin_bar ) )
454 $wp_admin_bar->add_menu( array(
456 'title' => $post_type_object->labels->view_item,
457 'href' => get_permalink( $post->ID )
459 } elseif ( 'edit-tags' == $current_screen->base
460 && isset( $tag ) && is_object( $tag )
461 && ( $tax = get_taxonomy( $tag->taxonomy ) )
464 $wp_admin_bar->add_menu( array(
466 'title' => $tax->labels->view_item,
467 'href' => get_term_link( $tag )
471 $current_object = $wp_the_query->get_queried_object();
473 if ( empty( $current_object ) )
476 if ( ! empty( $current_object->post_type )
477 && ( $post_type_object = get_post_type_object( $current_object->post_type ) )
478 && current_user_can( 'edit_post', $current_object->ID )
479 && $post_type_object->show_ui && $post_type_object->show_in_admin_bar )
481 $wp_admin_bar->add_menu( array(
483 'title' => $post_type_object->labels->edit_item,
484 'href' => get_edit_post_link( $current_object->ID )
486 } elseif ( ! empty( $current_object->taxonomy )
487 && ( $tax = get_taxonomy( $current_object->taxonomy ) )
488 && current_user_can( $tax->cap->edit_terms )
491 $wp_admin_bar->add_menu( array(
493 'title' => $tax->labels->edit_item,
494 'href' => get_edit_term_link( $current_object->term_id, $current_object->taxonomy )
501 * Add "Add New" menu.
505 * @param WP_Admin_Bar $wp_admin_bar
507 function wp_admin_bar_new_content_menu( $wp_admin_bar ) {
510 $cpts = (array) get_post_types( array( 'show_in_admin_bar' => true ), 'objects' );
512 if ( isset( $cpts['post'] ) && current_user_can( $cpts['post']->cap->create_posts ) )
513 $actions[ 'post-new.php' ] = array( $cpts['post']->labels->name_admin_bar, 'new-post' );
515 if ( isset( $cpts['attachment'] ) && current_user_can( 'upload_files' ) )
516 $actions[ 'media-new.php' ] = array( $cpts['attachment']->labels->name_admin_bar, 'new-media' );
518 if ( current_user_can( 'manage_links' ) )
519 $actions[ 'link-add.php' ] = array( _x( 'Link', 'add new from admin bar' ), 'new-link' );
521 if ( isset( $cpts['page'] ) && current_user_can( $cpts['page']->cap->create_posts ) )
522 $actions[ 'post-new.php?post_type=page' ] = array( $cpts['page']->labels->name_admin_bar, 'new-page' );
524 unset( $cpts['post'], $cpts['page'], $cpts['attachment'] );
526 // Add any additional custom post types.
527 foreach ( $cpts as $cpt ) {
528 if ( ! current_user_can( $cpt->cap->create_posts ) )
531 $key = 'post-new.php?post_type=' . $cpt->name;
532 $actions[ $key ] = array( $cpt->labels->name_admin_bar, 'new-' . $cpt->name );
534 // Avoid clash with parent node and a 'content' post type.
535 if ( isset( $actions['post-new.php?post_type=content'] ) )
536 $actions['post-new.php?post_type=content'][1] = 'add-new-content';
538 if ( current_user_can( 'create_users' ) || current_user_can( 'promote_users' ) )
539 $actions[ 'user-new.php' ] = array( _x( 'User', 'add new from admin bar' ), 'new-user' );
544 $title = '<span class="ab-icon"></span><span class="ab-label">' . _x( 'New', 'admin bar menu group label' ) . '</span>';
546 $wp_admin_bar->add_menu( array(
547 'id' => 'new-content',
549 'href' => admin_url( current( array_keys( $actions ) ) ),
551 'title' => _x( 'Add New', 'admin bar menu group label' ),
555 foreach ( $actions as $link => $action ) {
556 list( $title, $id ) = $action;
558 $wp_admin_bar->add_menu( array(
559 'parent' => 'new-content',
562 'href' => admin_url( $link )
568 * Add edit comments link with awaiting moderation count bubble.
572 * @param WP_Admin_Bar $wp_admin_bar
574 function wp_admin_bar_comments_menu( $wp_admin_bar ) {
575 if ( !current_user_can('edit_posts') )
578 $awaiting_mod = wp_count_comments();
579 $awaiting_mod = $awaiting_mod->moderated;
580 $awaiting_title = esc_attr( sprintf( _n( '%s comment awaiting moderation', '%s comments awaiting moderation', $awaiting_mod ), number_format_i18n( $awaiting_mod ) ) );
582 $icon = '<span class="ab-icon"></span>';
583 $title = '<span id="ab-awaiting-mod" class="ab-label awaiting-mod pending-count count-' . $awaiting_mod . '">' . number_format_i18n( $awaiting_mod ) . '</span>';
585 $wp_admin_bar->add_menu( array(
587 'title' => $icon . $title,
588 'href' => admin_url('edit-comments.php'),
589 'meta' => array( 'title' => $awaiting_title ),
594 * Add appearance submenu items to the "Site Name" menu.
598 * @param WP_Admin_Bar $wp_admin_bar
600 function wp_admin_bar_appearance_menu( $wp_admin_bar ) {
601 $wp_admin_bar->add_group( array( 'parent' => 'site-name', 'id' => 'appearance' ) );
603 if ( current_user_can( 'switch_themes' ) || current_user_can( 'edit_theme_options' ) )
604 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'themes', 'title' => __('Themes'), 'href' => admin_url('themes.php') ) );
606 if ( ! current_user_can( 'edit_theme_options' ) )
609 $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
610 $wp_admin_bar->add_menu( array(
611 'parent' => 'appearance',
613 'title' => __('Customize'),
614 'href' => add_query_arg( 'url', urlencode( $current_url ), wp_customize_url() ),
616 'class' => 'hide-if-no-customize',
619 add_action( 'wp_before_admin_bar_render', 'wp_customize_support_script' );
621 if ( current_theme_supports( 'widgets' ) )
622 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'widgets', 'title' => __('Widgets'), 'href' => admin_url('widgets.php') ) );
624 if ( current_theme_supports( 'menus' ) || current_theme_supports( 'widgets' ) )
625 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'menus', 'title' => __('Menus'), 'href' => admin_url('nav-menus.php') ) );
627 if ( current_theme_supports( 'custom-background' ) )
628 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'background', 'title' => __('Background'), 'href' => admin_url('themes.php?page=custom-background') ) );
630 if ( current_theme_supports( 'custom-header' ) )
631 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'header', 'title' => __('Header'), 'href' => admin_url('themes.php?page=custom-header') ) );
635 * Provide an update link if theme/plugin/core updates are available.
639 * @param WP_Admin_Bar $wp_admin_bar
641 function wp_admin_bar_updates_menu( $wp_admin_bar ) {
643 $update_data = wp_get_update_data();
645 if ( !$update_data['counts']['total'] )
648 $title = '<span class="ab-icon"></span><span class="ab-label">' . number_format_i18n( $update_data['counts']['total'] ) . '</span>';
649 $title .= '<span class="screen-reader-text">' . $update_data['title'] . '</span>';
651 $wp_admin_bar->add_menu( array(
654 'href' => network_admin_url( 'update-core.php' ),
656 'title' => $update_data['title'],
666 * @param WP_Admin_Bar $wp_admin_bar
668 function wp_admin_bar_search_menu( $wp_admin_bar ) {
672 $form = '<form action="' . esc_url( home_url( '/' ) ) . '" method="get" id="adminbarsearch">';
673 $form .= '<input class="adminbar-input" name="s" id="adminbar-search" type="text" value="" maxlength="150" />';
674 $form .= '<input type="submit" class="adminbar-button" value="' . __('Search') . '"/>';
677 $wp_admin_bar->add_menu( array(
678 'parent' => 'top-secondary',
682 'class' => 'admin-bar-search',
689 * Add secondary menus.
693 * @param WP_Admin_Bar $wp_admin_bar
695 function wp_admin_bar_add_secondary_groups( $wp_admin_bar ) {
696 $wp_admin_bar->add_group( array(
697 'id' => 'top-secondary',
699 'class' => 'ab-top-secondary',
703 $wp_admin_bar->add_group( array(
704 'parent' => 'wp-logo',
705 'id' => 'wp-logo-external',
707 'class' => 'ab-sub-secondary',
713 * Style and scripts for the admin bar.
717 function wp_admin_bar_header() { ?>
718 <style type="text/css" media="print">#wpadminbar { display:none; }</style>
723 * Default admin bar callback.
727 function _admin_bar_bump_cb() { ?>
728 <style type="text/css" media="screen">
729 html { margin-top: 28px !important; }
730 * html body { margin-top: 28px !important; }
736 * Set the display status of the admin bar.
738 * This can be called immediately upon plugin load. It does not need to be called from a function hooked to the init action.
742 * @param bool $show Whether to allow the admin bar to show.
745 function show_admin_bar( $show ) {
746 global $show_admin_bar;
747 $show_admin_bar = (bool) $show;
751 * Determine whether the admin bar should be showing.
755 * @return bool Whether the admin bar should be showing.
757 function is_admin_bar_showing() {
758 global $show_admin_bar, $pagenow;
760 // For all these types of requests, we never want an admin bar.
761 if ( defined('XMLRPC_REQUEST') || defined('DOING_AJAX') || defined('IFRAME_REQUEST') )
764 // Integrated into the admin.
768 if ( ! isset( $show_admin_bar ) ) {
769 if ( ! is_user_logged_in() || 'wp-login.php' == $pagenow ) {
770 $show_admin_bar = false;
772 $show_admin_bar = _get_admin_bar_pref();
776 $show_admin_bar = apply_filters( 'show_admin_bar', $show_admin_bar );
778 return $show_admin_bar;
782 * Retrieve the admin bar display preference of a user.
787 * @param string $context Context of this preference check. Defaults to 'front'. The 'admin'
788 * preference is no longer used.
789 * @param int $user Optional. ID of the user to check, defaults to 0 for current user.
790 * @return bool Whether the admin bar should be showing for this user.
792 function _get_admin_bar_pref( $context = 'front', $user = 0 ) {
793 $pref = get_user_option( "show_admin_bar_{$context}", $user );
794 if ( false === $pref )
797 return 'true' === $pref;