3 * Toolbar API: Top-level Toolbar functionality
11 * Instantiate the admin bar object and set it up as a global for access elsewhere.
13 * UNHOOKING THIS FUNCTION WILL NOT PROPERLY REMOVE THE ADMIN BAR.
14 * For that, use show_admin_bar(false) or the {@see 'show_admin_bar'} filter.
19 * @global WP_Admin_Bar $wp_admin_bar
21 * @return bool Whether the admin bar was successfully initialized.
23 function _wp_admin_bar_init() {
26 if ( ! is_admin_bar_showing() )
29 /* Load the admin bar class code ready for instantiation */
30 require_once( ABSPATH . WPINC . '/class-wp-admin-bar.php' );
32 /* Instantiate the admin bar */
35 * Filters the admin bar class to instantiate.
39 * @param string $wp_admin_bar_class Admin bar class to use. Default 'WP_Admin_Bar'.
41 $admin_bar_class = apply_filters( 'wp_admin_bar_class', 'WP_Admin_Bar' );
42 if ( class_exists( $admin_bar_class ) )
43 $wp_admin_bar = new $admin_bar_class;
47 $wp_admin_bar->initialize();
48 $wp_admin_bar->add_menus();
54 * Renders the admin bar to the page based on the $wp_admin_bar->menu member var.
56 * This is called very late on the footer actions so that it will render after
57 * anything else being added to the footer.
59 * It includes the {@see 'admin_bar_menu'} action which should be used to hook in and
60 * add new menus to the admin bar. That way you can be sure that you are adding at most
61 * optimal point, right before the admin bar is rendered. This also gives you access to
62 * the `$post` global, among others.
66 * @global WP_Admin_Bar $wp_admin_bar
68 function wp_admin_bar_render() {
71 if ( ! is_admin_bar_showing() || ! is_object( $wp_admin_bar ) )
75 * Load all necessary admin bar items.
77 * This is the hook used to add, remove, or manipulate admin bar items.
81 * @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance, passed by reference
83 do_action_ref_array( 'admin_bar_menu', array( &$wp_admin_bar ) );
86 * Fires before the admin bar is rendered.
90 do_action( 'wp_before_admin_bar_render' );
92 $wp_admin_bar->render();
95 * Fires after the admin bar is rendered.
99 do_action( 'wp_after_admin_bar_render' );
103 * Add the WordPress logo menu.
107 * @param WP_Admin_Bar $wp_admin_bar
109 function wp_admin_bar_wp_menu( $wp_admin_bar ) {
110 if ( current_user_can( 'read' ) ) {
111 $about_url = self_admin_url( 'about.php' );
112 } elseif ( is_multisite() ) {
113 $about_url = get_dashboard_url( get_current_user_id(), 'about.php' );
118 $wp_logo_menu_args = array(
120 'title' => '<span class="ab-icon"></span><span class="screen-reader-text">' . __( 'About WordPress' ) . '</span>',
121 'href' => $about_url,
124 // Set tabindex="0" to make sub menus accessible when no URL is available.
125 if ( ! $about_url ) {
126 $wp_logo_menu_args['meta'] = array(
131 $wp_admin_bar->add_menu( $wp_logo_menu_args );
134 // Add "About WordPress" link
135 $wp_admin_bar->add_menu( array(
136 'parent' => 'wp-logo',
138 'title' => __('About WordPress'),
139 'href' => $about_url,
143 // Add WordPress.org link
144 $wp_admin_bar->add_menu( array(
145 'parent' => 'wp-logo-external',
147 'title' => __('WordPress.org'),
148 'href' => __('https://wordpress.org/'),
152 $wp_admin_bar->add_menu( array(
153 'parent' => 'wp-logo-external',
154 'id' => 'documentation',
155 'title' => __('Documentation'),
156 'href' => __('https://codex.wordpress.org/'),
160 $wp_admin_bar->add_menu( array(
161 'parent' => 'wp-logo-external',
162 'id' => 'support-forums',
163 'title' => __('Support Forums'),
164 'href' => __('https://wordpress.org/support/'),
168 $wp_admin_bar->add_menu( array(
169 'parent' => 'wp-logo-external',
171 'title' => __('Feedback'),
172 'href' => __('https://wordpress.org/support/forum/requests-and-feedback'),
177 * Add the sidebar toggle button.
181 * @param WP_Admin_Bar $wp_admin_bar
183 function wp_admin_bar_sidebar_toggle( $wp_admin_bar ) {
185 $wp_admin_bar->add_menu( array(
186 'id' => 'menu-toggle',
187 'title' => '<span class="ab-icon"></span><span class="screen-reader-text">' . __( 'Menu' ) . '</span>',
194 * Add the "My Account" item.
198 * @param WP_Admin_Bar $wp_admin_bar
200 function wp_admin_bar_my_account_item( $wp_admin_bar ) {
201 $user_id = get_current_user_id();
202 $current_user = wp_get_current_user();
207 if ( current_user_can( 'read' ) ) {
208 $profile_url = get_edit_profile_url( $user_id );
209 } elseif ( is_multisite() ) {
210 $profile_url = get_dashboard_url( $user_id, 'profile.php' );
212 $profile_url = false;
215 $avatar = get_avatar( $user_id, 26 );
216 /* translators: %s: current user's display name */
217 $howdy = sprintf( __( 'Howdy, %s' ), $current_user->display_name );
218 $class = empty( $avatar ) ? '' : 'with-avatar';
220 $wp_admin_bar->add_menu( array(
221 'id' => 'my-account',
222 'parent' => 'top-secondary',
223 'title' => $howdy . $avatar,
224 'href' => $profile_url,
232 * Add the "My Account" submenu items.
236 * @param WP_Admin_Bar $wp_admin_bar
238 function wp_admin_bar_my_account_menu( $wp_admin_bar ) {
239 $user_id = get_current_user_id();
240 $current_user = wp_get_current_user();
245 if ( current_user_can( 'read' ) ) {
246 $profile_url = get_edit_profile_url( $user_id );
247 } elseif ( is_multisite() ) {
248 $profile_url = get_dashboard_url( $user_id, 'profile.php' );
250 $profile_url = false;
253 $wp_admin_bar->add_group( array(
254 'parent' => 'my-account',
255 'id' => 'user-actions',
258 $user_info = get_avatar( $user_id, 64 );
259 $user_info .= "<span class='display-name'>{$current_user->display_name}</span>";
261 if ( $current_user->display_name !== $current_user->user_login )
262 $user_info .= "<span class='username'>{$current_user->user_login}</span>";
264 $wp_admin_bar->add_menu( array(
265 'parent' => 'user-actions',
267 'title' => $user_info,
268 'href' => $profile_url,
274 if ( false !== $profile_url ) {
275 $wp_admin_bar->add_menu( array(
276 'parent' => 'user-actions',
277 'id' => 'edit-profile',
278 'title' => __( 'Edit My Profile' ),
279 'href' => $profile_url,
283 $wp_admin_bar->add_menu( array(
284 'parent' => 'user-actions',
286 'title' => __( 'Log Out' ),
287 'href' => wp_logout_url(),
292 * Add the "Site Name" menu.
296 * @param WP_Admin_Bar $wp_admin_bar
298 function wp_admin_bar_site_menu( $wp_admin_bar ) {
299 // Don't show for logged out users.
300 if ( ! is_user_logged_in() )
303 // Show only when the user is a member of this site, or they're a super admin.
304 if ( ! is_user_member_of_blog() && ! is_super_admin() )
307 $blogname = get_bloginfo('name');
310 $blogname = preg_replace( '#^(https?://)?(www.)?#', '', get_home_url() );
313 if ( is_network_admin() ) {
314 /* translators: %s: site name */
315 $blogname = sprintf( __( 'Network Admin: %s' ), esc_html( get_network()->site_name ) );
316 } elseif ( is_user_admin() ) {
317 /* translators: %s: site name */
318 $blogname = sprintf( __( 'User Dashboard: %s' ), esc_html( get_network()->site_name ) );
321 $title = wp_html_excerpt( $blogname, 40, '…' );
323 $wp_admin_bar->add_menu( array(
326 'href' => ( is_admin() || ! current_user_can( 'read' ) ) ? home_url( '/' ) : admin_url(),
329 // Create submenu items.
332 // Add an option to visit the site.
333 $wp_admin_bar->add_menu( array(
334 'parent' => 'site-name',
336 'title' => __( 'Visit Site' ),
337 'href' => home_url( '/' ),
340 if ( is_blog_admin() && is_multisite() && current_user_can( 'manage_sites' ) ) {
341 $wp_admin_bar->add_menu( array(
342 'parent' => 'site-name',
344 'title' => __( 'Edit Site' ),
345 'href' => network_admin_url( 'site-info.php?id=' . get_current_blog_id() ),
349 } else if ( current_user_can( 'read' ) ) {
350 // We're on the front end, link to the Dashboard.
351 $wp_admin_bar->add_menu( array(
352 'parent' => 'site-name',
354 'title' => __( 'Dashboard' ),
355 'href' => admin_url(),
358 // Add the appearance submenu items.
359 wp_admin_bar_appearance_menu( $wp_admin_bar );
364 * Adds the "Customize" link to the Toolbar.
368 * @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance.
369 * @global WP_Customize_Manager $wp_customize
371 function wp_admin_bar_customize_menu( $wp_admin_bar ) {
372 global $wp_customize;
374 // Don't show for users who can't access the customizer or when in the admin.
375 if ( ! current_user_can( 'customize' ) || is_admin() ) {
379 // Don't show if the user cannot edit a given customize_changeset post currently being previewed.
380 if ( is_customize_preview() && $wp_customize->changeset_post_id() && ! current_user_can( get_post_type_object( 'customize_changeset' )->cap->edit_post, $wp_customize->changeset_post_id() ) ) {
384 $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
385 if ( is_customize_preview() && $wp_customize->changeset_uuid() ) {
386 $current_url = remove_query_arg( 'customize_changeset_uuid', $current_url );
389 $customize_url = add_query_arg( 'url', urlencode( $current_url ), wp_customize_url() );
390 if ( is_customize_preview() ) {
391 $customize_url = add_query_arg( array( 'changeset_uuid' => $wp_customize->changeset_uuid() ), $customize_url );
394 $wp_admin_bar->add_menu( array(
396 'title' => __( 'Customize' ),
397 'href' => $customize_url,
399 'class' => 'hide-if-no-customize',
402 add_action( 'wp_before_admin_bar_render', 'wp_customize_support_script' );
406 * Add the "My Sites/[Site Name]" menu and all submenus.
410 * @param WP_Admin_Bar $wp_admin_bar
412 function wp_admin_bar_my_sites_menu( $wp_admin_bar ) {
413 // Don't show for logged out users or single site mode.
414 if ( ! is_user_logged_in() || ! is_multisite() )
417 // Show only when the user has at least one site, or they're a super admin.
418 if ( count( $wp_admin_bar->user->blogs ) < 1 && ! is_super_admin() )
421 if ( $wp_admin_bar->user->active_blog ) {
422 $my_sites_url = get_admin_url( $wp_admin_bar->user->active_blog->blog_id, 'my-sites.php' );
424 $my_sites_url = admin_url( 'my-sites.php' );
427 $wp_admin_bar->add_menu( array(
429 'title' => __( 'My Sites' ),
430 'href' => $my_sites_url,
433 if ( is_super_admin() ) {
434 $wp_admin_bar->add_group( array(
435 'parent' => 'my-sites',
436 'id' => 'my-sites-super-admin',
439 $wp_admin_bar->add_menu( array(
440 'parent' => 'my-sites-super-admin',
441 'id' => 'network-admin',
442 'title' => __('Network Admin'),
443 'href' => network_admin_url(),
446 $wp_admin_bar->add_menu( array(
447 'parent' => 'network-admin',
448 'id' => 'network-admin-d',
449 'title' => __( 'Dashboard' ),
450 'href' => network_admin_url(),
452 $wp_admin_bar->add_menu( array(
453 'parent' => 'network-admin',
454 'id' => 'network-admin-s',
455 'title' => __( 'Sites' ),
456 'href' => network_admin_url( 'sites.php' ),
458 $wp_admin_bar->add_menu( array(
459 'parent' => 'network-admin',
460 'id' => 'network-admin-u',
461 'title' => __( 'Users' ),
462 'href' => network_admin_url( 'users.php' ),
464 $wp_admin_bar->add_menu( array(
465 'parent' => 'network-admin',
466 'id' => 'network-admin-t',
467 'title' => __( 'Themes' ),
468 'href' => network_admin_url( 'themes.php' ),
470 $wp_admin_bar->add_menu( array(
471 'parent' => 'network-admin',
472 'id' => 'network-admin-p',
473 'title' => __( 'Plugins' ),
474 'href' => network_admin_url( 'plugins.php' ),
476 $wp_admin_bar->add_menu( array(
477 'parent' => 'network-admin',
478 'id' => 'network-admin-o',
479 'title' => __( 'Settings' ),
480 'href' => network_admin_url( 'settings.php' ),
485 $wp_admin_bar->add_group( array(
486 'parent' => 'my-sites',
487 'id' => 'my-sites-list',
489 'class' => is_super_admin() ? 'ab-sub-secondary' : '',
493 foreach ( (array) $wp_admin_bar->user->blogs as $blog ) {
494 switch_to_blog( $blog->userblog_id );
496 $blavatar = '<div class="blavatar"></div>';
498 $blogname = $blog->blogname;
501 $blogname = preg_replace( '#^(https?://)?(www.)?#', '', get_home_url() );
504 $menu_id = 'blog-' . $blog->userblog_id;
506 $wp_admin_bar->add_menu( array(
507 'parent' => 'my-sites-list',
509 'title' => $blavatar . $blogname,
510 'href' => admin_url(),
513 $wp_admin_bar->add_menu( array(
514 'parent' => $menu_id,
515 'id' => $menu_id . '-d',
516 'title' => __( 'Dashboard' ),
517 'href' => admin_url(),
520 if ( current_user_can( get_post_type_object( 'post' )->cap->create_posts ) ) {
521 $wp_admin_bar->add_menu( array(
522 'parent' => $menu_id,
523 'id' => $menu_id . '-n',
524 'title' => __( 'New Post' ),
525 'href' => admin_url( 'post-new.php' ),
529 if ( current_user_can( 'edit_posts' ) ) {
530 $wp_admin_bar->add_menu( array(
531 'parent' => $menu_id,
532 'id' => $menu_id . '-c',
533 'title' => __( 'Manage Comments' ),
534 'href' => admin_url( 'edit-comments.php' ),
538 $wp_admin_bar->add_menu( array(
539 'parent' => $menu_id,
540 'id' => $menu_id . '-v',
541 'title' => __( 'Visit Site' ),
542 'href' => home_url( '/' ),
545 restore_current_blog();
550 * Provide a shortlink.
554 * @param WP_Admin_Bar $wp_admin_bar
556 function wp_admin_bar_shortlink_menu( $wp_admin_bar ) {
557 $short = wp_get_shortlink( 0, 'query' );
558 $id = 'get-shortlink';
560 if ( empty( $short ) )
563 $html = '<input class="shortlink-input" type="text" readonly="readonly" value="' . esc_attr( $short ) . '" />';
565 $wp_admin_bar->add_menu( array(
567 'title' => __( 'Shortlink' ),
569 'meta' => array( 'html' => $html ),
574 * Provide an edit link for posts and terms.
578 * @global WP_Term $tag
579 * @global WP_Query $wp_the_query
581 * @param WP_Admin_Bar $wp_admin_bar
583 function wp_admin_bar_edit_menu( $wp_admin_bar ) {
584 global $tag, $wp_the_query;
587 $current_screen = get_current_screen();
590 if ( 'post' == $current_screen->base
591 && 'add' != $current_screen->action
592 && ( $post_type_object = get_post_type_object( $post->post_type ) )
593 && current_user_can( 'read_post', $post->ID )
594 && ( $post_type_object->public )
595 && ( $post_type_object->show_in_admin_bar ) )
597 if ( 'draft' == $post->post_status ) {
598 $preview_link = get_preview_post_link( $post );
599 $wp_admin_bar->add_menu( array(
601 'title' => $post_type_object->labels->view_item,
602 'href' => esc_url( $preview_link ),
603 'meta' => array( 'target' => 'wp-preview-' . $post->ID ),
606 $wp_admin_bar->add_menu( array(
608 'title' => $post_type_object->labels->view_item,
609 'href' => get_permalink( $post->ID )
612 } elseif ( 'edit' == $current_screen->base
613 && ( $post_type_object = get_post_type_object( $current_screen->post_type ) )
614 && ( $post_type_object->public )
615 && ( $post_type_object->show_in_admin_bar )
616 && ( get_post_type_archive_link( $post_type_object->name ) )
617 && ! ( 'post' === $post_type_object->name && 'posts' === get_option( 'show_on_front' ) ) )
619 $wp_admin_bar->add_node( array(
621 'title' => $post_type_object->labels->view_items,
622 'href' => get_post_type_archive_link( $current_screen->post_type )
624 } elseif ( 'term' == $current_screen->base
625 && isset( $tag ) && is_object( $tag ) && ! is_wp_error( $tag )
626 && ( $tax = get_taxonomy( $tag->taxonomy ) )
629 $wp_admin_bar->add_menu( array(
631 'title' => $tax->labels->view_item,
632 'href' => get_term_link( $tag )
636 $current_object = $wp_the_query->get_queried_object();
638 if ( empty( $current_object ) )
641 if ( ! empty( $current_object->post_type )
642 && ( $post_type_object = get_post_type_object( $current_object->post_type ) )
643 && current_user_can( 'edit_post', $current_object->ID )
644 && $post_type_object->show_in_admin_bar
645 && $edit_post_link = get_edit_post_link( $current_object->ID ) )
647 $wp_admin_bar->add_menu( array(
649 'title' => $post_type_object->labels->edit_item,
650 'href' => $edit_post_link
652 } elseif ( ! empty( $current_object->taxonomy )
653 && ( $tax = get_taxonomy( $current_object->taxonomy ) )
654 && current_user_can( 'edit_term', $current_object->term_id )
655 && $edit_term_link = get_edit_term_link( $current_object->term_id, $current_object->taxonomy ) )
657 $wp_admin_bar->add_menu( array(
659 'title' => $tax->labels->edit_item,
660 'href' => $edit_term_link
667 * Add "Add New" menu.
671 * @param WP_Admin_Bar $wp_admin_bar
673 function wp_admin_bar_new_content_menu( $wp_admin_bar ) {
676 $cpts = (array) get_post_types( array( 'show_in_admin_bar' => true ), 'objects' );
678 if ( isset( $cpts['post'] ) && current_user_can( $cpts['post']->cap->create_posts ) )
679 $actions[ 'post-new.php' ] = array( $cpts['post']->labels->name_admin_bar, 'new-post' );
681 if ( isset( $cpts['attachment'] ) && current_user_can( 'upload_files' ) )
682 $actions[ 'media-new.php' ] = array( $cpts['attachment']->labels->name_admin_bar, 'new-media' );
684 if ( current_user_can( 'manage_links' ) )
685 $actions[ 'link-add.php' ] = array( _x( 'Link', 'add new from admin bar' ), 'new-link' );
687 if ( isset( $cpts['page'] ) && current_user_can( $cpts['page']->cap->create_posts ) )
688 $actions[ 'post-new.php?post_type=page' ] = array( $cpts['page']->labels->name_admin_bar, 'new-page' );
690 unset( $cpts['post'], $cpts['page'], $cpts['attachment'] );
692 // Add any additional custom post types.
693 foreach ( $cpts as $cpt ) {
694 if ( ! current_user_can( $cpt->cap->create_posts ) )
697 $key = 'post-new.php?post_type=' . $cpt->name;
698 $actions[ $key ] = array( $cpt->labels->name_admin_bar, 'new-' . $cpt->name );
700 // Avoid clash with parent node and a 'content' post type.
701 if ( isset( $actions['post-new.php?post_type=content'] ) )
702 $actions['post-new.php?post_type=content'][1] = 'add-new-content';
704 if ( current_user_can( 'create_users' ) || current_user_can( 'promote_users' ) )
705 $actions[ 'user-new.php' ] = array( _x( 'User', 'add new from admin bar' ), 'new-user' );
710 $title = '<span class="ab-icon"></span><span class="ab-label">' . _x( 'New', 'admin bar menu group label' ) . '</span>';
712 $wp_admin_bar->add_menu( array(
713 'id' => 'new-content',
715 'href' => admin_url( current( array_keys( $actions ) ) ),
718 foreach ( $actions as $link => $action ) {
719 list( $title, $id ) = $action;
721 $wp_admin_bar->add_menu( array(
722 'parent' => 'new-content',
725 'href' => admin_url( $link )
731 * Add edit comments link with awaiting moderation count bubble.
735 * @param WP_Admin_Bar $wp_admin_bar
737 function wp_admin_bar_comments_menu( $wp_admin_bar ) {
738 if ( !current_user_can('edit_posts') )
741 $awaiting_mod = wp_count_comments();
742 $awaiting_mod = $awaiting_mod->moderated;
743 $awaiting_text = sprintf( _n( '%s comment awaiting moderation', '%s comments awaiting moderation', $awaiting_mod ), number_format_i18n( $awaiting_mod ) );
745 $icon = '<span class="ab-icon"></span>';
746 $title = '<span class="ab-label awaiting-mod pending-count count-' . $awaiting_mod . '" aria-hidden="true">' . number_format_i18n( $awaiting_mod ) . '</span>';
747 $title .= '<span class="screen-reader-text">' . $awaiting_text . '</span>';
749 $wp_admin_bar->add_menu( array(
751 'title' => $icon . $title,
752 'href' => admin_url('edit-comments.php'),
757 * Add appearance submenu items to the "Site Name" menu.
761 * @param WP_Admin_Bar $wp_admin_bar
763 function wp_admin_bar_appearance_menu( $wp_admin_bar ) {
764 $wp_admin_bar->add_group( array( 'parent' => 'site-name', 'id' => 'appearance' ) );
766 if ( current_user_can( 'switch_themes' ) ) {
767 $wp_admin_bar->add_menu( array(
768 'parent' => 'appearance',
770 'title' => __( 'Themes' ),
771 'href' => admin_url( 'themes.php' ),
775 if ( ! current_user_can( 'edit_theme_options' ) ) {
779 if ( current_theme_supports( 'widgets' ) ) {
780 $wp_admin_bar->add_menu( array(
781 'parent' => 'appearance',
783 'title' => __( 'Widgets' ),
784 'href' => admin_url( 'widgets.php' ),
788 if ( current_theme_supports( 'menus' ) || current_theme_supports( 'widgets' ) )
789 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'menus', 'title' => __('Menus'), 'href' => admin_url('nav-menus.php') ) );
791 if ( current_theme_supports( 'custom-background' ) ) {
792 $wp_admin_bar->add_menu( array(
793 'parent' => 'appearance',
794 'id' => 'background',
795 'title' => __( 'Background' ),
796 'href' => admin_url( 'themes.php?page=custom-background' ),
798 'class' => 'hide-if-customize',
803 if ( current_theme_supports( 'custom-header' ) ) {
804 $wp_admin_bar->add_menu( array(
805 'parent' => 'appearance',
807 'title' => __( 'Header' ),
808 'href' => admin_url( 'themes.php?page=custom-header' ),
810 'class' => 'hide-if-customize',
818 * Provide an update link if theme/plugin/core updates are available.
822 * @param WP_Admin_Bar $wp_admin_bar
824 function wp_admin_bar_updates_menu( $wp_admin_bar ) {
826 $update_data = wp_get_update_data();
828 if ( !$update_data['counts']['total'] )
831 $title = '<span class="ab-icon"></span><span class="ab-label">' . number_format_i18n( $update_data['counts']['total'] ) . '</span>';
832 $title .= '<span class="screen-reader-text">' . $update_data['title'] . '</span>';
834 $wp_admin_bar->add_menu( array(
837 'href' => network_admin_url( 'update-core.php' ),
839 'title' => $update_data['title'],
849 * @param WP_Admin_Bar $wp_admin_bar
851 function wp_admin_bar_search_menu( $wp_admin_bar ) {
855 $form = '<form action="' . esc_url( home_url( '/' ) ) . '" method="get" id="adminbarsearch">';
856 $form .= '<input class="adminbar-input" name="s" id="adminbar-search" type="text" value="" maxlength="150" />';
857 $form .= '<label for="adminbar-search" class="screen-reader-text">' . __( 'Search' ) . '</label>';
858 $form .= '<input type="submit" class="adminbar-button" value="' . __('Search') . '"/>';
861 $wp_admin_bar->add_menu( array(
862 'parent' => 'top-secondary',
866 'class' => 'admin-bar-search',
873 * Add secondary menus.
877 * @param WP_Admin_Bar $wp_admin_bar
879 function wp_admin_bar_add_secondary_groups( $wp_admin_bar ) {
880 $wp_admin_bar->add_group( array(
881 'id' => 'top-secondary',
883 'class' => 'ab-top-secondary',
887 $wp_admin_bar->add_group( array(
888 'parent' => 'wp-logo',
889 'id' => 'wp-logo-external',
891 'class' => 'ab-sub-secondary',
897 * Style and scripts for the admin bar.
901 function wp_admin_bar_header() { ?>
902 <style type="text/css" media="print">#wpadminbar { display:none; }</style>
907 * Default admin bar callback.
911 function _admin_bar_bump_cb() { ?>
912 <style type="text/css" media="screen">
913 html { margin-top: 32px !important; }
914 * html body { margin-top: 32px !important; }
915 @media screen and ( max-width: 782px ) {
916 html { margin-top: 46px !important; }
917 * html body { margin-top: 46px !important; }
924 * Sets the display status of the admin bar.
926 * This can be called immediately upon plugin load. It does not need to be called
927 * from a function hooked to the {@see 'init'} action.
931 * @global bool $show_admin_bar
933 * @param bool $show Whether to allow the admin bar to show.
935 function show_admin_bar( $show ) {
936 global $show_admin_bar;
937 $show_admin_bar = (bool) $show;
941 * Determine whether the admin bar should be showing.
945 * @global bool $show_admin_bar
946 * @global string $pagenow
948 * @return bool Whether the admin bar should be showing.
950 function is_admin_bar_showing() {
951 global $show_admin_bar, $pagenow;
953 // For all these types of requests, we never want an admin bar.
954 if ( defined('XMLRPC_REQUEST') || defined('DOING_AJAX') || defined('IFRAME_REQUEST') )
961 // Integrated into the admin.
965 if ( ! isset( $show_admin_bar ) ) {
966 if ( ! is_user_logged_in() || 'wp-login.php' == $pagenow ) {
967 $show_admin_bar = false;
969 $show_admin_bar = _get_admin_bar_pref();
974 * Filters whether to show the admin bar.
976 * Returning false to this hook is the recommended way to hide the admin bar.
977 * The user's display preference is used for logged in users.
981 * @param bool $show_admin_bar Whether the admin bar should be shown. Default false.
983 $show_admin_bar = apply_filters( 'show_admin_bar', $show_admin_bar );
985 return $show_admin_bar;
989 * Retrieve the admin bar display preference of a user.
994 * @param string $context Context of this preference check. Defaults to 'front'. The 'admin'
995 * preference is no longer used.
996 * @param int $user Optional. ID of the user to check, defaults to 0 for current user.
997 * @return bool Whether the admin bar should be showing for this user.
999 function _get_admin_bar_pref( $context = 'front', $user = 0 ) {
1000 $pref = get_user_option( "show_admin_bar_{$context}", $user );
1001 if ( false === $pref )
1004 return 'true' === $pref;