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 * To hide the admin bar, you're looking in the wrong place. Unhooking this function will not
12 * properly remove the admin bar. 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 add_action( 'init', '_wp_admin_bar_init' ); // Don't remove. Wrong way to disable.
42 * Render the admin bar to the page based on the $wp_admin_bar->menu member var.
43 * This is called very late on the footer actions so that it will render after anything else being
44 * added to the footer.
46 * It includes the action "admin_bar_menu" which should be used to hook in and
47 * add new menus to the admin bar. That way you can be sure that you are adding at most optimal point,
48 * right before the admin bar is rendered. This also gives you access to the $post global, among others.
52 function wp_admin_bar_render() {
55 if ( ! is_admin_bar_showing() || ! is_object( $wp_admin_bar ) )
58 do_action_ref_array( 'admin_bar_menu', array( &$wp_admin_bar ) );
60 do_action( 'wp_before_admin_bar_render' );
62 $wp_admin_bar->render();
64 do_action( 'wp_after_admin_bar_render' );
66 add_action( 'wp_footer', 'wp_admin_bar_render', 1000 );
67 add_action( 'in_admin_header', 'wp_admin_bar_render', 0 );
70 * Add the WordPress logo menu.
74 function wp_admin_bar_wp_menu( $wp_admin_bar ) {
75 $wp_admin_bar->add_menu( array(
77 'title' => '<span class="ab-icon"></span>',
78 'href' => self_admin_url( 'about.php' ),
80 'title' => __('About WordPress'),
84 if ( is_user_logged_in() ) {
85 // Add "About WordPress" link
86 $wp_admin_bar->add_menu( array(
87 'parent' => 'wp-logo',
89 'title' => __('About WordPress'),
90 'href' => self_admin_url( 'about.php' ),
94 // Add WordPress.org link
95 $wp_admin_bar->add_menu( array(
96 'parent' => 'wp-logo-external',
98 'title' => __('WordPress.org'),
99 'href' => __('http://wordpress.org/'),
103 $wp_admin_bar->add_menu( array(
104 'parent' => 'wp-logo-external',
105 'id' => 'documentation',
106 'title' => __('Documentation'),
107 'href' => __('http://codex.wordpress.org/'),
111 $wp_admin_bar->add_menu( array(
112 'parent' => 'wp-logo-external',
113 'id' => 'support-forums',
114 'title' => __('Support Forums'),
115 'href' => __('http://wordpress.org/support/'),
119 $wp_admin_bar->add_menu( array(
120 'parent' => 'wp-logo-external',
122 'title' => __('Feedback'),
123 'href' => __('http://wordpress.org/support/forum/requests-and-feedback'),
128 * Add the "My Account" item.
132 function wp_admin_bar_my_account_item( $wp_admin_bar ) {
133 $user_id = get_current_user_id();
134 $current_user = wp_get_current_user();
135 $profile_url = get_edit_profile_url( $user_id );
140 $avatar = get_avatar( $user_id, 16 );
141 $howdy = sprintf( __('Howdy, %1$s'), $current_user->display_name );
142 $class = empty( $avatar ) ? '' : 'with-avatar';
144 $wp_admin_bar->add_menu( array(
145 'id' => 'my-account',
146 'parent' => 'top-secondary',
147 'title' => $howdy . $avatar,
148 'href' => $profile_url,
151 'title' => __('My Account'),
157 * Add the "My Account" submenu items.
161 function wp_admin_bar_my_account_menu( $wp_admin_bar ) {
162 $user_id = get_current_user_id();
163 $current_user = wp_get_current_user();
164 $profile_url = get_edit_profile_url( $user_id );
169 $wp_admin_bar->add_group( array(
170 'parent' => 'my-account',
171 'id' => 'user-actions',
174 $user_info = get_avatar( $user_id, 64 );
175 $user_info .= "<span class='display-name'>{$current_user->display_name}</span>";
177 if ( $current_user->display_name !== $current_user->user_nicename )
178 $user_info .= "<span class='username'>{$current_user->user_nicename}</span>";
180 $wp_admin_bar->add_menu( array(
181 'parent' => 'user-actions',
183 'title' => $user_info,
184 'href' => $profile_url,
189 $wp_admin_bar->add_menu( array(
190 'parent' => 'user-actions',
191 'id' => 'edit-profile',
192 'title' => __( 'Edit My Profile' ),
193 'href' => $profile_url,
195 $wp_admin_bar->add_menu( array(
196 'parent' => 'user-actions',
198 'title' => __( 'Log Out' ),
199 'href' => wp_logout_url(),
204 * Add the "Site Name" menu.
208 function wp_admin_bar_site_menu( $wp_admin_bar ) {
209 global $current_site;
211 // Don't show for logged out users.
212 if ( ! is_user_logged_in() )
215 // Show only when the user is a member of this site, or they're a super admin.
216 if ( ! is_user_member_of_blog() && ! is_super_admin() )
219 $blogname = get_bloginfo('name');
221 if ( empty( $blogname ) )
222 $blogname = preg_replace( '#^(https?://)?(www.)?#', '', get_home_url() );
224 if ( is_network_admin() ) {
225 $blogname = sprintf( __('Network Admin: %s'), esc_html( $current_site->site_name ) );
226 } elseif ( is_user_admin() ) {
227 $blogname = sprintf( __('Global Dashboard: %s'), esc_html( $current_site->site_name ) );
230 $title = wp_html_excerpt( $blogname, 40 );
231 if ( $title != $blogname )
232 $title = trim( $title ) . '…';
234 $wp_admin_bar->add_menu( array(
237 'href' => is_admin() ? home_url( '/' ) : admin_url(),
240 // Create submenu items.
243 // Add an option to visit the site.
244 $wp_admin_bar->add_menu( array(
245 'parent' => 'site-name',
247 'title' => __( 'Visit Site' ),
248 'href' => home_url( '/' ),
251 if ( is_blog_admin() && is_multisite() && current_user_can( 'manage_sites' ) ) {
252 $wp_admin_bar->add_menu( array(
253 'parent' => 'site-name',
255 'title' => __( 'Edit Site' ),
256 'href' => network_admin_url( 'site-info.php?id=' . get_current_blog_id() ),
261 // We're on the front end, link to the Dashboard.
262 $wp_admin_bar->add_menu( array(
263 'parent' => 'site-name',
265 'title' => __( 'Dashboard' ),
266 'href' => admin_url(),
269 // Add the appearance submenu items.
270 wp_admin_bar_appearance_menu( $wp_admin_bar );
275 * Add the "My Sites/[Site Name]" menu and all submenus.
279 function wp_admin_bar_my_sites_menu( $wp_admin_bar ) {
282 // Don't show for logged out users or single site mode.
283 if ( ! is_user_logged_in() || ! is_multisite() )
286 // Show only when the user has at least one site, or they're a super admin.
287 if ( count( $wp_admin_bar->user->blogs ) < 1 && ! is_super_admin() )
290 $wp_admin_bar->add_menu( array(
292 'title' => __( 'My Sites' ),
293 'href' => admin_url( 'my-sites.php' ),
296 if ( is_super_admin() ) {
297 $wp_admin_bar->add_group( array(
298 'parent' => 'my-sites',
299 'id' => 'my-sites-super-admin',
302 $wp_admin_bar->add_menu( array(
303 'parent' => 'my-sites-super-admin',
304 'id' => 'network-admin',
305 'title' => __('Network Admin'),
306 'href' => network_admin_url(),
309 $wp_admin_bar->add_menu( array(
310 'parent' => 'network-admin',
311 'id' => 'network-admin-d',
312 'title' => __( 'Dashboard' ),
313 'href' => network_admin_url(),
315 $wp_admin_bar->add_menu( array(
316 'parent' => 'network-admin',
317 'id' => 'network-admin-s',
318 'title' => __( 'Sites' ),
319 'href' => network_admin_url( 'sites.php' ),
321 $wp_admin_bar->add_menu( array(
322 'parent' => 'network-admin',
323 'id' => 'network-admin-u',
324 'title' => __( 'Users' ),
325 'href' => network_admin_url( 'users.php' ),
327 $wp_admin_bar->add_menu( array(
328 'parent' => 'network-admin',
329 'id' => 'network-admin-v',
330 'title' => __( 'Visit Network' ),
331 'href' => network_home_url(),
336 $wp_admin_bar->add_group( array(
337 'parent' => 'my-sites',
338 'id' => 'my-sites-list',
340 'class' => is_super_admin() ? 'ab-sub-secondary' : '',
344 $blue_wp_logo_url = includes_url('images/wpmini-blue.png');
346 foreach ( (array) $wp_admin_bar->user->blogs as $blog ) {
347 // @todo Replace with some favicon lookup.
348 //$blavatar = '<img src="' . esc_url( blavatar_url( blavatar_domain( $blog->siteurl ), 'img', 16, $blue_wp_logo_url ) ) . '" alt="Blavatar" width="16" height="16" />';
349 $blavatar = '<img src="' . esc_url($blue_wp_logo_url) . '" alt="' . esc_attr__( 'Blavatar' ) . '" width="16" height="16" class="blavatar"/>';
351 $blogname = empty( $blog->blogname ) ? $blog->domain : $blog->blogname;
352 $menu_id = 'blog-' . $blog->userblog_id;
354 $wp_admin_bar->add_menu( array(
355 'parent' => 'my-sites-list',
357 'title' => $blavatar . $blogname,
358 'href' => get_admin_url( $blog->userblog_id ),
361 $wp_admin_bar->add_menu( array(
362 'parent' => $menu_id,
363 'id' => $menu_id . '-d',
364 'title' => __( 'Dashboard' ),
365 'href' => get_admin_url( $blog->userblog_id ),
368 if ( current_user_can_for_blog( $blog->userblog_id, 'edit_posts' ) ) {
369 $wp_admin_bar->add_menu( array(
370 'parent' => $menu_id,
371 'id' => $menu_id . '-n',
372 'title' => __( 'New Post' ),
373 'href' => get_admin_url( $blog->userblog_id, 'post-new.php' ),
375 $wp_admin_bar->add_menu( array(
376 'parent' => $menu_id,
377 'id' => $menu_id . '-c',
378 'title' => __( 'Manage Comments' ),
379 'href' => get_admin_url( $blog->userblog_id, 'edit-comments.php' ),
383 $wp_admin_bar->add_menu( array(
384 'parent' => $menu_id,
385 'id' => $menu_id . '-v',
386 'title' => __( 'Visit Site' ),
387 'href' => get_home_url( $blog->userblog_id, '/' ),
393 * Provide a shortlink.
397 function wp_admin_bar_shortlink_menu( $wp_admin_bar ) {
398 $short = wp_get_shortlink( 0, 'query' );
399 $id = 'get-shortlink';
401 if ( empty( $short ) )
404 $html = '<input class="shortlink-input" type="text" readonly="readonly" value="' . esc_attr( $short ) . '" />';
406 $wp_admin_bar->add_menu( array(
408 'title' => __( 'Shortlink' ),
410 'meta' => array( 'html' => $html ),
415 * Provide an edit link for posts and terms.
419 function wp_admin_bar_edit_menu( $wp_admin_bar ) {
420 global $post, $tag, $wp_the_query;
423 $current_screen = get_current_screen();
425 if ( 'post' == $current_screen->base
426 && 'add' != $current_screen->action
427 && ( $post_type_object = get_post_type_object( $post->post_type ) )
428 && current_user_can( $post_type_object->cap->read_post, $post->ID )
429 && ( $post_type_object->public ) )
431 $wp_admin_bar->add_menu( array(
433 'title' => $post_type_object->labels->view_item,
434 'href' => get_permalink( $post->ID )
436 } elseif ( 'edit-tags' == $current_screen->base
437 && isset( $tag ) && is_object( $tag )
438 && ( $tax = get_taxonomy( $tag->taxonomy ) )
441 $wp_admin_bar->add_menu( array(
443 'title' => $tax->labels->view_item,
444 'href' => get_term_link( $tag )
448 $current_object = $wp_the_query->get_queried_object();
450 if ( empty( $current_object ) )
453 if ( ! empty( $current_object->post_type )
454 && ( $post_type_object = get_post_type_object( $current_object->post_type ) )
455 && current_user_can( $post_type_object->cap->edit_post, $current_object->ID )
456 && ( $post_type_object->show_ui || 'attachment' == $current_object->post_type ) )
458 $wp_admin_bar->add_menu( array(
460 'title' => $post_type_object->labels->edit_item,
461 'href' => get_edit_post_link( $current_object->ID )
463 } elseif ( ! empty( $current_object->taxonomy )
464 && ( $tax = get_taxonomy( $current_object->taxonomy ) )
465 && current_user_can( $tax->cap->edit_terms )
468 $wp_admin_bar->add_menu( array(
470 'title' => $tax->labels->edit_item,
471 'href' => get_edit_term_link( $current_object->term_id, $current_object->taxonomy )
478 * Add "Add New" menu.
482 function wp_admin_bar_new_content_menu( $wp_admin_bar ) {
485 $cpts = (array) get_post_types( array( 'show_in_admin_bar' => true ), 'objects' );
487 if ( isset( $cpts['post'] ) && current_user_can( $cpts['post']->cap->edit_posts ) ) {
488 $actions[ 'post-new.php' ] = array( $cpts['post']->labels->name_admin_bar, 'new-post' );
489 unset( $cpts['post'] );
492 if ( current_user_can( 'upload_files' ) )
493 $actions[ 'media-new.php' ] = array( _x( 'Media', 'add new from admin bar' ), 'new-media' );
495 if ( current_user_can( 'manage_links' ) )
496 $actions[ 'link-add.php' ] = array( _x( 'Link', 'add new from admin bar' ), 'new-link' );
498 if ( isset( $cpts['page'] ) && current_user_can( $cpts['page']->cap->edit_posts ) ) {
499 $actions[ 'post-new.php?post_type=page' ] = array( $cpts['page']->labels->name_admin_bar, 'new-page' );
500 unset( $cpts['page'] );
503 // Add any additional custom post types.
504 foreach ( $cpts as $cpt ) {
505 if ( ! current_user_can( $cpt->cap->edit_posts ) )
508 $key = 'post-new.php?post_type=' . $cpt->name;
509 $actions[ $key ] = array( $cpt->labels->name_admin_bar, 'new-' . $cpt->name );
512 if ( current_user_can( 'create_users' ) || current_user_can( 'promote_users' ) )
513 $actions[ 'user-new.php' ] = array( _x( 'User', 'add new from admin bar' ), 'new-user' );
518 $title = '<span class="ab-icon"></span><span class="ab-label">' . _x( 'New', 'admin bar menu group label' ) . '</span>';
520 $wp_admin_bar->add_menu( array(
521 'id' => 'new-content',
523 'href' => admin_url( current( array_keys( $actions ) ) ),
525 'title' => _x( 'Add New', 'admin bar menu group label' ),
529 foreach ( $actions as $link => $action ) {
530 list( $title, $id ) = $action;
532 $wp_admin_bar->add_menu( array(
533 'parent' => 'new-content',
536 'href' => admin_url( $link )
542 * Add edit comments link with awaiting moderation count bubble.
546 function wp_admin_bar_comments_menu( $wp_admin_bar ) {
547 if ( !current_user_can('edit_posts') )
550 $awaiting_mod = wp_count_comments();
551 $awaiting_mod = $awaiting_mod->moderated;
552 $awaiting_title = esc_attr( sprintf( _n( '%s comment awaiting moderation', '%s comments awaiting moderation', $awaiting_mod ), number_format_i18n( $awaiting_mod ) ) );
554 $icon = '<span class="ab-icon"></span>';
555 $title = '<span id="ab-awaiting-mod" class="ab-label awaiting-mod pending-count count-' . $awaiting_mod . '">' . number_format_i18n( $awaiting_mod ) . '</span>';
557 $wp_admin_bar->add_menu( array(
559 'title' => $icon . $title,
560 'href' => admin_url('edit-comments.php'),
561 'meta' => array( 'title' => $awaiting_title ),
566 * Add appearance submenu items to the "Site Name" menu.
570 function wp_admin_bar_appearance_menu( $wp_admin_bar ) {
571 $wp_admin_bar->add_group( array( 'parent' => 'site-name', 'id' => 'appearance' ) );
573 if ( current_user_can( 'switch_themes' ) || current_user_can( 'edit_theme_options' ) )
574 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'themes', 'title' => __('Themes'), 'href' => admin_url('themes.php') ) );
576 if ( ! current_user_can( 'edit_theme_options' ) )
579 $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
580 $wp_admin_bar->add_menu( array(
581 'parent' => 'appearance',
583 'title' => __('Customize'),
584 'href' => add_query_arg( 'url', urlencode( $current_url ), wp_customize_url() ),
586 'class' => 'hide-if-no-customize',
589 add_action( 'wp_before_admin_bar_render', 'wp_customize_support_script' );
591 if ( current_theme_supports( 'widgets' ) )
592 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'widgets', 'title' => __('Widgets'), 'href' => admin_url('widgets.php') ) );
594 if ( current_theme_supports( 'menus' ) || current_theme_supports( 'widgets' ) )
595 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'menus', 'title' => __('Menus'), 'href' => admin_url('nav-menus.php') ) );
597 if ( current_theme_supports( 'custom-background' ) )
598 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'background', 'title' => __('Background'), 'href' => admin_url('themes.php?page=custom-background') ) );
600 if ( current_theme_supports( 'custom-header' ) )
601 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'header', 'title' => __('Header'), 'href' => admin_url('themes.php?page=custom-header') ) );
605 * Provide an update link if theme/plugin/core updates are available.
609 function wp_admin_bar_updates_menu( $wp_admin_bar ) {
611 $update_data = wp_get_update_data();
613 if ( !$update_data['counts']['total'] )
616 $title = '<span class="ab-icon"></span><span class="ab-label">' . number_format_i18n( $update_data['counts']['total'] ) . '</span>';
618 $wp_admin_bar->add_menu( array(
621 'href' => network_admin_url( 'update-core.php' ),
623 'title' => $update_data['title'],
633 function wp_admin_bar_search_menu( $wp_admin_bar ) {
637 $form = '<form action="' . esc_url( home_url( '/' ) ) . '" method="get" id="adminbarsearch">';
638 $form .= '<input class="adminbar-input" name="s" id="adminbar-search" tabindex="10" type="text" value="" maxlength="150" />';
639 $form .= '<input type="submit" class="adminbar-button" value="' . __('Search') . '"/>';
642 $wp_admin_bar->add_menu( array(
643 'parent' => 'top-secondary',
647 'class' => 'admin-bar-search',
654 * Add secondary menus.
658 function wp_admin_bar_add_secondary_groups( $wp_admin_bar ) {
659 $wp_admin_bar->add_group( array(
660 'id' => 'top-secondary',
662 'class' => 'ab-top-secondary',
666 $wp_admin_bar->add_group( array(
667 'parent' => 'wp-logo',
668 'id' => 'wp-logo-external',
670 'class' => 'ab-sub-secondary',
676 * Style and scripts for the admin bar.
681 function wp_admin_bar_header() { ?>
682 <style type="text/css" media="print">#wpadminbar { display:none; }</style>
687 * Default admin bar callback.
692 function _admin_bar_bump_cb() { ?>
693 <style type="text/css" media="screen">
694 html { margin-top: 28px !important; }
695 * html body { margin-top: 28px !important; }
701 * Set the display status of the admin bar.
703 * This can be called immediately upon plugin load. It does not need to be called from a function hooked to the init action.
707 * @param bool $show Whether to allow the admin bar to show.
710 function show_admin_bar( $show ) {
711 global $show_admin_bar;
712 $show_admin_bar = (bool) $show;
716 * Determine whether the admin bar should be showing.
720 * @return bool Whether the admin bar should be showing.
722 function is_admin_bar_showing() {
723 global $show_admin_bar, $pagenow;
725 // For all these types of requests, we never want an admin bar.
726 if ( defined('XMLRPC_REQUEST') || defined('APP_REQUEST') || defined('DOING_AJAX') || defined('IFRAME_REQUEST') )
729 // Integrated into the admin.
733 if ( ! isset( $show_admin_bar ) ) {
734 if ( ! is_user_logged_in() || 'wp-login.php' == $pagenow ) {
735 $show_admin_bar = false;
737 $show_admin_bar = _get_admin_bar_pref();
741 $show_admin_bar = apply_filters( 'show_admin_bar', $show_admin_bar );
743 return $show_admin_bar;
747 * Retrieve the admin bar display preference of a user.
752 * @param string $context Context of this preference check. Defaults to 'front'. The 'admin'
753 * preference is no longer used.
754 * @param int $user Optional. ID of the user to check, defaults to 0 for current user.
755 * @return bool Whether the admin bar should be showing for this user.
757 function _get_admin_bar_pref( $context = 'front', $user = 0 ) {
758 $pref = get_user_option( "show_admin_bar_{$context}", $user );
759 if ( false === $pref )
762 return 'true' === $pref;