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 function wp_admin_bar_wp_menu( $wp_admin_bar ) {
77 $wp_admin_bar->add_menu( array(
79 'title' => '<span class="ab-icon"></span>',
80 'href' => self_admin_url( 'about.php' ),
82 'title' => __('About WordPress'),
86 if ( is_user_logged_in() ) {
87 // Add "About WordPress" link
88 $wp_admin_bar->add_menu( array(
89 'parent' => 'wp-logo',
91 'title' => __('About WordPress'),
92 'href' => self_admin_url( 'about.php' ),
96 // Add WordPress.org link
97 $wp_admin_bar->add_menu( array(
98 'parent' => 'wp-logo-external',
100 'title' => __('WordPress.org'),
101 'href' => __('http://wordpress.org/'),
105 $wp_admin_bar->add_menu( array(
106 'parent' => 'wp-logo-external',
107 'id' => 'documentation',
108 'title' => __('Documentation'),
109 'href' => __('http://codex.wordpress.org/'),
113 $wp_admin_bar->add_menu( array(
114 'parent' => 'wp-logo-external',
115 'id' => 'support-forums',
116 'title' => __('Support Forums'),
117 'href' => __('http://wordpress.org/support/'),
121 $wp_admin_bar->add_menu( array(
122 'parent' => 'wp-logo-external',
124 'title' => __('Feedback'),
125 'href' => __('http://wordpress.org/support/forum/requests-and-feedback'),
130 * Add the "My Account" item.
134 function wp_admin_bar_my_account_item( $wp_admin_bar ) {
135 $user_id = get_current_user_id();
136 $current_user = wp_get_current_user();
137 $profile_url = get_edit_profile_url( $user_id );
142 $avatar = get_avatar( $user_id, 16 );
143 $howdy = sprintf( __('Howdy, %1$s'), $current_user->display_name );
144 $class = empty( $avatar ) ? '' : 'with-avatar';
146 $wp_admin_bar->add_menu( array(
147 'id' => 'my-account',
148 'parent' => 'top-secondary',
149 'title' => $howdy . $avatar,
150 'href' => $profile_url,
153 'title' => __('My Account'),
159 * Add the "My Account" submenu items.
163 function wp_admin_bar_my_account_menu( $wp_admin_bar ) {
164 $user_id = get_current_user_id();
165 $current_user = wp_get_current_user();
166 $profile_url = get_edit_profile_url( $user_id );
171 $wp_admin_bar->add_group( array(
172 'parent' => 'my-account',
173 'id' => 'user-actions',
176 $user_info = get_avatar( $user_id, 64 );
177 $user_info .= "<span class='display-name'>{$current_user->display_name}</span>";
179 if ( $current_user->display_name !== $current_user->user_login )
180 $user_info .= "<span class='username'>{$current_user->user_login}</span>";
182 $wp_admin_bar->add_menu( array(
183 'parent' => 'user-actions',
185 'title' => $user_info,
186 'href' => $profile_url,
191 $wp_admin_bar->add_menu( array(
192 'parent' => 'user-actions',
193 'id' => 'edit-profile',
194 'title' => __( 'Edit My Profile' ),
195 'href' => $profile_url,
197 $wp_admin_bar->add_menu( array(
198 'parent' => 'user-actions',
200 'title' => __( 'Log Out' ),
201 'href' => wp_logout_url(),
206 * Add the "Site Name" menu.
210 function wp_admin_bar_site_menu( $wp_admin_bar ) {
211 global $current_site;
213 // Don't show for logged out users.
214 if ( ! is_user_logged_in() )
217 // Show only when the user is a member of this site, or they're a super admin.
218 if ( ! is_user_member_of_blog() && ! is_super_admin() )
221 $blogname = get_bloginfo('name');
223 if ( empty( $blogname ) )
224 $blogname = preg_replace( '#^(https?://)?(www.)?#', '', get_home_url() );
226 if ( is_network_admin() ) {
227 $blogname = sprintf( __('Network Admin: %s'), esc_html( $current_site->site_name ) );
228 } elseif ( is_user_admin() ) {
229 $blogname = sprintf( __('Global Dashboard: %s'), esc_html( $current_site->site_name ) );
232 $title = wp_html_excerpt( $blogname, 40, '…' );
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 foreach ( (array) $wp_admin_bar->user->blogs as $blog ) {
345 switch_to_blog( $blog->userblog_id );
347 $blavatar = '<div class="blavatar"></div>';
349 $blogname = empty( $blog->blogname ) ? $blog->domain : $blog->blogname;
350 $menu_id = 'blog-' . $blog->userblog_id;
352 $wp_admin_bar->add_menu( array(
353 'parent' => 'my-sites-list',
355 'title' => $blavatar . $blogname,
356 'href' => admin_url(),
359 $wp_admin_bar->add_menu( array(
360 'parent' => $menu_id,
361 'id' => $menu_id . '-d',
362 'title' => __( 'Dashboard' ),
363 'href' => admin_url(),
366 if ( current_user_can( get_post_type_object( 'post' )->cap->create_posts ) ) {
367 $wp_admin_bar->add_menu( array(
368 'parent' => $menu_id,
369 'id' => $menu_id . '-n',
370 'title' => __( 'New Post' ),
371 'href' => admin_url( 'post-new.php' ),
375 if ( current_user_can( 'edit_posts' ) ) {
376 $wp_admin_bar->add_menu( array(
377 'parent' => $menu_id,
378 'id' => $menu_id . '-c',
379 'title' => __( 'Manage Comments' ),
380 'href' => admin_url( 'edit-comments.php' ),
384 $wp_admin_bar->add_menu( array(
385 'parent' => $menu_id,
386 'id' => $menu_id . '-v',
387 'title' => __( 'Visit Site' ),
388 'href' => home_url( '/' ),
391 restore_current_blog();
396 * Provide a shortlink.
400 function wp_admin_bar_shortlink_menu( $wp_admin_bar ) {
401 $short = wp_get_shortlink( 0, 'query' );
402 $id = 'get-shortlink';
404 if ( empty( $short ) )
407 $html = '<input class="shortlink-input" type="text" readonly="readonly" value="' . esc_attr( $short ) . '" />';
409 $wp_admin_bar->add_menu( array(
411 'title' => __( 'Shortlink' ),
413 'meta' => array( 'html' => $html ),
418 * Provide an edit link for posts and terms.
422 function wp_admin_bar_edit_menu( $wp_admin_bar ) {
423 global $tag, $wp_the_query;
426 $current_screen = get_current_screen();
429 if ( 'post' == $current_screen->base
430 && 'add' != $current_screen->action
431 && ( $post_type_object = get_post_type_object( $post->post_type ) )
432 && current_user_can( 'read_post', $post->ID )
433 && ( $post_type_object->public )
434 && ( $post_type_object->show_in_admin_bar ) )
436 $wp_admin_bar->add_menu( array(
438 'title' => $post_type_object->labels->view_item,
439 'href' => get_permalink( $post->ID )
441 } elseif ( 'edit-tags' == $current_screen->base
442 && isset( $tag ) && is_object( $tag )
443 && ( $tax = get_taxonomy( $tag->taxonomy ) )
446 $wp_admin_bar->add_menu( array(
448 'title' => $tax->labels->view_item,
449 'href' => get_term_link( $tag )
453 $current_object = $wp_the_query->get_queried_object();
455 if ( empty( $current_object ) )
458 if ( ! empty( $current_object->post_type )
459 && ( $post_type_object = get_post_type_object( $current_object->post_type ) )
460 && current_user_can( 'edit_post', $current_object->ID )
461 && $post_type_object->show_ui && $post_type_object->show_in_admin_bar )
463 $wp_admin_bar->add_menu( array(
465 'title' => $post_type_object->labels->edit_item,
466 'href' => get_edit_post_link( $current_object->ID )
468 } elseif ( ! empty( $current_object->taxonomy )
469 && ( $tax = get_taxonomy( $current_object->taxonomy ) )
470 && current_user_can( $tax->cap->edit_terms )
473 $wp_admin_bar->add_menu( array(
475 'title' => $tax->labels->edit_item,
476 'href' => get_edit_term_link( $current_object->term_id, $current_object->taxonomy )
483 * Add "Add New" menu.
487 function wp_admin_bar_new_content_menu( $wp_admin_bar ) {
490 $cpts = (array) get_post_types( array( 'show_in_admin_bar' => true ), 'objects' );
492 if ( isset( $cpts['post'] ) && current_user_can( $cpts['post']->cap->create_posts ) )
493 $actions[ 'post-new.php' ] = array( $cpts['post']->labels->name_admin_bar, 'new-post' );
495 if ( isset( $cpts['attachment'] ) && current_user_can( 'upload_files' ) )
496 $actions[ 'media-new.php' ] = array( $cpts['attachment']->labels->name_admin_bar, 'new-media' );
498 if ( current_user_can( 'manage_links' ) )
499 $actions[ 'link-add.php' ] = array( _x( 'Link', 'add new from admin bar' ), 'new-link' );
501 if ( isset( $cpts['page'] ) && current_user_can( $cpts['page']->cap->create_posts ) )
502 $actions[ 'post-new.php?post_type=page' ] = array( $cpts['page']->labels->name_admin_bar, 'new-page' );
504 unset( $cpts['post'], $cpts['page'], $cpts['attachment'] );
506 // Add any additional custom post types.
507 foreach ( $cpts as $cpt ) {
508 if ( ! current_user_can( $cpt->cap->create_posts ) )
511 $key = 'post-new.php?post_type=' . $cpt->name;
512 $actions[ $key ] = array( $cpt->labels->name_admin_bar, 'new-' . $cpt->name );
514 // Avoid clash with parent node and a 'content' post type.
515 if ( isset( $actions['post-new.php?post_type=content'] ) )
516 $actions['post-new.php?post_type=content'][1] = 'add-new-content';
518 if ( current_user_can( 'create_users' ) || current_user_can( 'promote_users' ) )
519 $actions[ 'user-new.php' ] = array( _x( 'User', 'add new from admin bar' ), 'new-user' );
524 $title = '<span class="ab-icon"></span><span class="ab-label">' . _x( 'New', 'admin bar menu group label' ) . '</span>';
526 $wp_admin_bar->add_menu( array(
527 'id' => 'new-content',
529 'href' => admin_url( current( array_keys( $actions ) ) ),
531 'title' => _x( 'Add New', 'admin bar menu group label' ),
535 foreach ( $actions as $link => $action ) {
536 list( $title, $id ) = $action;
538 $wp_admin_bar->add_menu( array(
539 'parent' => 'new-content',
542 'href' => admin_url( $link )
548 * Add edit comments link with awaiting moderation count bubble.
552 function wp_admin_bar_comments_menu( $wp_admin_bar ) {
553 if ( !current_user_can('edit_posts') )
556 $awaiting_mod = wp_count_comments();
557 $awaiting_mod = $awaiting_mod->moderated;
558 $awaiting_title = esc_attr( sprintf( _n( '%s comment awaiting moderation', '%s comments awaiting moderation', $awaiting_mod ), number_format_i18n( $awaiting_mod ) ) );
560 $icon = '<span class="ab-icon"></span>';
561 $title = '<span id="ab-awaiting-mod" class="ab-label awaiting-mod pending-count count-' . $awaiting_mod . '">' . number_format_i18n( $awaiting_mod ) . '</span>';
563 $wp_admin_bar->add_menu( array(
565 'title' => $icon . $title,
566 'href' => admin_url('edit-comments.php'),
567 'meta' => array( 'title' => $awaiting_title ),
572 * Add appearance submenu items to the "Site Name" menu.
576 function wp_admin_bar_appearance_menu( $wp_admin_bar ) {
577 $wp_admin_bar->add_group( array( 'parent' => 'site-name', 'id' => 'appearance' ) );
579 if ( current_user_can( 'switch_themes' ) || current_user_can( 'edit_theme_options' ) )
580 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'themes', 'title' => __('Themes'), 'href' => admin_url('themes.php') ) );
582 if ( ! current_user_can( 'edit_theme_options' ) )
585 $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
586 $wp_admin_bar->add_menu( array(
587 'parent' => 'appearance',
589 'title' => __('Customize'),
590 'href' => add_query_arg( 'url', urlencode( $current_url ), wp_customize_url() ),
592 'class' => 'hide-if-no-customize',
595 add_action( 'wp_before_admin_bar_render', 'wp_customize_support_script' );
597 if ( current_theme_supports( 'widgets' ) )
598 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'widgets', 'title' => __('Widgets'), 'href' => admin_url('widgets.php') ) );
600 if ( current_theme_supports( 'menus' ) || current_theme_supports( 'widgets' ) )
601 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'menus', 'title' => __('Menus'), 'href' => admin_url('nav-menus.php') ) );
603 if ( current_theme_supports( 'custom-background' ) )
604 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'background', 'title' => __('Background'), 'href' => admin_url('themes.php?page=custom-background') ) );
606 if ( current_theme_supports( 'custom-header' ) )
607 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'header', 'title' => __('Header'), 'href' => admin_url('themes.php?page=custom-header') ) );
611 * Provide an update link if theme/plugin/core updates are available.
615 function wp_admin_bar_updates_menu( $wp_admin_bar ) {
617 $update_data = wp_get_update_data();
619 if ( !$update_data['counts']['total'] )
622 $title = '<span class="ab-icon"></span><span class="ab-label">' . number_format_i18n( $update_data['counts']['total'] ) . '</span>';
623 $title .= '<span class="screen-reader-text">' . $update_data['title'] . '</span>';
625 $wp_admin_bar->add_menu( array(
628 'href' => network_admin_url( 'update-core.php' ),
630 'title' => $update_data['title'],
640 function wp_admin_bar_search_menu( $wp_admin_bar ) {
644 $form = '<form action="' . esc_url( home_url( '/' ) ) . '" method="get" id="adminbarsearch">';
645 $form .= '<input class="adminbar-input" name="s" id="adminbar-search" type="text" value="" maxlength="150" />';
646 $form .= '<input type="submit" class="adminbar-button" value="' . __('Search') . '"/>';
649 $wp_admin_bar->add_menu( array(
650 'parent' => 'top-secondary',
654 'class' => 'admin-bar-search',
661 * Add secondary menus.
665 function wp_admin_bar_add_secondary_groups( $wp_admin_bar ) {
666 $wp_admin_bar->add_group( array(
667 'id' => 'top-secondary',
669 'class' => 'ab-top-secondary',
673 $wp_admin_bar->add_group( array(
674 'parent' => 'wp-logo',
675 'id' => 'wp-logo-external',
677 'class' => 'ab-sub-secondary',
683 * Style and scripts for the admin bar.
688 function wp_admin_bar_header() { ?>
689 <style type="text/css" media="print">#wpadminbar { display:none; }</style>
694 * Default admin bar callback.
699 function _admin_bar_bump_cb() { ?>
700 <style type="text/css" media="screen">
701 html { margin-top: 28px !important; }
702 * html body { margin-top: 28px !important; }
708 * Set the display status of the admin bar.
710 * This can be called immediately upon plugin load. It does not need to be called from a function hooked to the init action.
714 * @param bool $show Whether to allow the admin bar to show.
717 function show_admin_bar( $show ) {
718 global $show_admin_bar;
719 $show_admin_bar = (bool) $show;
723 * Determine whether the admin bar should be showing.
727 * @return bool Whether the admin bar should be showing.
729 function is_admin_bar_showing() {
730 global $show_admin_bar, $pagenow;
732 // For all these types of requests, we never want an admin bar.
733 if ( defined('XMLRPC_REQUEST') || defined('DOING_AJAX') || defined('IFRAME_REQUEST') )
736 // Integrated into the admin.
740 if ( ! isset( $show_admin_bar ) ) {
741 if ( ! is_user_logged_in() || 'wp-login.php' == $pagenow ) {
742 $show_admin_bar = false;
744 $show_admin_bar = _get_admin_bar_pref();
748 $show_admin_bar = apply_filters( 'show_admin_bar', $show_admin_bar );
750 return $show_admin_bar;
754 * Retrieve the admin bar display preference of a user.
759 * @param string $context Context of this preference check. Defaults to 'front'. The 'admin'
760 * preference is no longer used.
761 * @param int $user Optional. ID of the user to check, defaults to 0 for current user.
762 * @return bool Whether the admin bar should be showing for this user.
764 function _get_admin_bar_pref( $context = 'front', $user = 0 ) {
765 $pref = get_user_option( "show_admin_bar_{$context}", $user );
766 if ( false === $pref )
769 return 'true' === $pref;