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_once( 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();
49 * Render the admin bar to the page based on the $wp_admin_bar->menu member var.
50 * This is called very late on the footer actions so that it will render after anything else being
51 * added to the footer.
53 * It includes the action "admin_bar_menu" which should be used to hook in and
54 * add new menus to the admin bar. That way you can be sure that you are adding at most optimal point,
55 * right before the admin bar is rendered. This also gives you access to the $post global, among others.
59 function wp_admin_bar_render() {
62 if ( ! is_admin_bar_showing() || ! is_object( $wp_admin_bar ) )
66 * Load all necessary admin bar items.
68 * This is the hook used to add, remove, or manipulate admin bar items.
72 * @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance, passed by reference
74 do_action_ref_array( 'admin_bar_menu', array( &$wp_admin_bar ) );
77 * Fires before the admin bar is rendered.
81 do_action( 'wp_before_admin_bar_render' );
83 $wp_admin_bar->render();
86 * Fires after the admin bar is rendered.
90 do_action( 'wp_after_admin_bar_render' );
94 * Add the WordPress logo menu.
98 * @param WP_Admin_Bar $wp_admin_bar
100 function wp_admin_bar_wp_menu( $wp_admin_bar ) {
101 $wp_admin_bar->add_menu( array(
103 'title' => '<span class="ab-icon"></span><span class="screen-reader-text">' . __( 'About WordPress' ) . '</span>',
104 'href' => self_admin_url( 'about.php' ),
107 if ( is_user_logged_in() ) {
108 // Add "About WordPress" link
109 $wp_admin_bar->add_menu( array(
110 'parent' => 'wp-logo',
112 'title' => __('About WordPress'),
113 'href' => self_admin_url( 'about.php' ),
117 // Add WordPress.org link
118 $wp_admin_bar->add_menu( array(
119 'parent' => 'wp-logo-external',
121 'title' => __('WordPress.org'),
122 'href' => __('https://wordpress.org/'),
126 $wp_admin_bar->add_menu( array(
127 'parent' => 'wp-logo-external',
128 'id' => 'documentation',
129 'title' => __('Documentation'),
130 'href' => __('https://codex.wordpress.org/'),
134 $wp_admin_bar->add_menu( array(
135 'parent' => 'wp-logo-external',
136 'id' => 'support-forums',
137 'title' => __('Support Forums'),
138 'href' => __('https://wordpress.org/support/'),
142 $wp_admin_bar->add_menu( array(
143 'parent' => 'wp-logo-external',
145 'title' => __('Feedback'),
146 'href' => __('https://wordpress.org/support/forum/requests-and-feedback'),
151 * Add the sidebar toggle button.
155 * @param WP_Admin_Bar $wp_admin_bar
157 function wp_admin_bar_sidebar_toggle( $wp_admin_bar ) {
159 $wp_admin_bar->add_menu( array(
160 'id' => 'menu-toggle',
161 'title' => '<span class="ab-icon"></span><span class="screen-reader-text">' . __( 'Menu' ) . '</span>',
168 * Add the "My Account" item.
172 * @param WP_Admin_Bar $wp_admin_bar
174 function wp_admin_bar_my_account_item( $wp_admin_bar ) {
175 $user_id = get_current_user_id();
176 $current_user = wp_get_current_user();
177 $profile_url = get_edit_profile_url( $user_id );
182 $avatar = get_avatar( $user_id, 26 );
183 $howdy = sprintf( __('Howdy, %1$s'), $current_user->display_name );
184 $class = empty( $avatar ) ? '' : 'with-avatar';
186 $wp_admin_bar->add_menu( array(
187 'id' => 'my-account',
188 'parent' => 'top-secondary',
189 'title' => $howdy . $avatar,
190 'href' => $profile_url,
198 * Add the "My Account" submenu items.
202 * @param WP_Admin_Bar $wp_admin_bar
204 function wp_admin_bar_my_account_menu( $wp_admin_bar ) {
205 $user_id = get_current_user_id();
206 $current_user = wp_get_current_user();
207 $profile_url = get_edit_profile_url( $user_id );
212 $wp_admin_bar->add_group( array(
213 'parent' => 'my-account',
214 'id' => 'user-actions',
217 $user_info = get_avatar( $user_id, 64 );
218 $user_info .= "<span class='display-name'>{$current_user->display_name}</span>";
220 if ( $current_user->display_name !== $current_user->user_login )
221 $user_info .= "<span class='username'>{$current_user->user_login}</span>";
223 $wp_admin_bar->add_menu( array(
224 'parent' => 'user-actions',
226 'title' => $user_info,
227 'href' => $profile_url,
232 $wp_admin_bar->add_menu( array(
233 'parent' => 'user-actions',
234 'id' => 'edit-profile',
235 'title' => __( 'Edit My Profile' ),
236 'href' => $profile_url,
238 $wp_admin_bar->add_menu( array(
239 'parent' => 'user-actions',
241 'title' => __( 'Log Out' ),
242 'href' => wp_logout_url(),
247 * Add the "Site Name" menu.
251 * @param WP_Admin_Bar $wp_admin_bar
253 function wp_admin_bar_site_menu( $wp_admin_bar ) {
254 // Don't show for logged out users.
255 if ( ! is_user_logged_in() )
258 // Show only when the user is a member of this site, or they're a super admin.
259 if ( ! is_user_member_of_blog() && ! is_super_admin() )
262 $blogname = get_bloginfo('name');
265 $blogname = preg_replace( '#^(https?://)?(www.)?#', '', get_home_url() );
268 if ( is_network_admin() ) {
269 $blogname = sprintf( __('Network Admin: %s'), esc_html( get_current_site()->site_name ) );
270 } elseif ( is_user_admin() ) {
271 $blogname = sprintf( __('Global Dashboard: %s'), esc_html( get_current_site()->site_name ) );
274 $title = wp_html_excerpt( $blogname, 40, '…' );
276 $wp_admin_bar->add_menu( array(
279 'href' => is_admin() ? home_url( '/' ) : admin_url(),
282 // Create submenu items.
285 // Add an option to visit the site.
286 $wp_admin_bar->add_menu( array(
287 'parent' => 'site-name',
289 'title' => __( 'Visit Site' ),
290 'href' => home_url( '/' ),
293 if ( is_blog_admin() && is_multisite() && current_user_can( 'manage_sites' ) ) {
294 $wp_admin_bar->add_menu( array(
295 'parent' => 'site-name',
297 'title' => __( 'Edit Site' ),
298 'href' => network_admin_url( 'site-info.php?id=' . get_current_blog_id() ),
303 // We're on the front end, link to the Dashboard.
304 $wp_admin_bar->add_menu( array(
305 'parent' => 'site-name',
307 'title' => __( 'Dashboard' ),
308 'href' => admin_url(),
311 // Add the appearance submenu items.
312 wp_admin_bar_appearance_menu( $wp_admin_bar );
317 * Add the "My Sites/[Site Name]" menu and all submenus.
321 * @param WP_Admin_Bar $wp_admin_bar
323 function wp_admin_bar_my_sites_menu( $wp_admin_bar ) {
324 // Don't show for logged out users or single site mode.
325 if ( ! is_user_logged_in() || ! is_multisite() )
328 // Show only when the user has at least one site, or they're a super admin.
329 if ( count( $wp_admin_bar->user->blogs ) < 1 && ! is_super_admin() )
332 if ( $wp_admin_bar->user->active_blog ) {
333 $my_sites_url = get_admin_url( $wp_admin_bar->user->active_blog->blog_id, 'my-sites.php' );
335 $my_sites_url = admin_url( 'my-sites.php' );
338 $wp_admin_bar->add_menu( array(
340 'title' => __( 'My Sites' ),
341 'href' => $my_sites_url,
344 if ( is_super_admin() ) {
345 $wp_admin_bar->add_group( array(
346 'parent' => 'my-sites',
347 'id' => 'my-sites-super-admin',
350 $wp_admin_bar->add_menu( array(
351 'parent' => 'my-sites-super-admin',
352 'id' => 'network-admin',
353 'title' => __('Network Admin'),
354 'href' => network_admin_url(),
357 $wp_admin_bar->add_menu( array(
358 'parent' => 'network-admin',
359 'id' => 'network-admin-d',
360 'title' => __( 'Dashboard' ),
361 'href' => network_admin_url(),
363 $wp_admin_bar->add_menu( array(
364 'parent' => 'network-admin',
365 'id' => 'network-admin-s',
366 'title' => __( 'Sites' ),
367 'href' => network_admin_url( 'sites.php' ),
369 $wp_admin_bar->add_menu( array(
370 'parent' => 'network-admin',
371 'id' => 'network-admin-u',
372 'title' => __( 'Users' ),
373 'href' => network_admin_url( 'users.php' ),
375 $wp_admin_bar->add_menu( array(
376 'parent' => 'network-admin',
377 'id' => 'network-admin-t',
378 'title' => __( 'Themes' ),
379 'href' => network_admin_url( 'themes.php' ),
381 $wp_admin_bar->add_menu( array(
382 'parent' => 'network-admin',
383 'id' => 'network-admin-p',
384 'title' => __( 'Plugins' ),
385 'href' => network_admin_url( 'plugins.php' ),
390 $wp_admin_bar->add_group( array(
391 'parent' => 'my-sites',
392 'id' => 'my-sites-list',
394 'class' => is_super_admin() ? 'ab-sub-secondary' : '',
398 foreach ( (array) $wp_admin_bar->user->blogs as $blog ) {
399 switch_to_blog( $blog->userblog_id );
401 $blavatar = '<div class="blavatar"></div>';
403 $blogname = $blog->blogname;
406 $blogname = preg_replace( '#^(https?://)?(www.)?#', '', get_home_url() );
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 if( 'draft' == $post->post_status ) {
500 $preview_link = set_url_scheme( get_permalink( $post->ID ) );
501 /** This filter is documented in wp-admin/includes/meta-boxes.php */
502 $preview_link = apply_filters( 'preview_post_link', add_query_arg( 'preview', 'true', $preview_link ), $post );
503 $wp_admin_bar->add_menu( array(
505 'title' => $post_type_object->labels->view_item,
506 'href' => esc_url( $preview_link ),
507 'meta' => array( 'target' => 'wp-preview-' . $post->ID ),
510 $wp_admin_bar->add_menu( array(
512 'title' => $post_type_object->labels->view_item,
513 'href' => get_permalink( $post->ID )
516 } elseif ( 'edit-tags' == $current_screen->base
517 && isset( $tag ) && is_object( $tag )
518 && ( $tax = get_taxonomy( $tag->taxonomy ) )
521 $wp_admin_bar->add_menu( array(
523 'title' => $tax->labels->view_item,
524 'href' => get_term_link( $tag )
528 $current_object = $wp_the_query->get_queried_object();
530 if ( empty( $current_object ) )
533 if ( ! empty( $current_object->post_type )
534 && ( $post_type_object = get_post_type_object( $current_object->post_type ) )
535 && current_user_can( 'edit_post', $current_object->ID )
536 && $post_type_object->show_ui && $post_type_object->show_in_admin_bar
537 && $edit_post_link = get_edit_post_link( $current_object->ID ) )
539 $wp_admin_bar->add_menu( array(
541 'title' => $post_type_object->labels->edit_item,
542 'href' => $edit_post_link
544 } elseif ( ! empty( $current_object->taxonomy )
545 && ( $tax = get_taxonomy( $current_object->taxonomy ) )
546 && current_user_can( $tax->cap->edit_terms )
548 && $edit_term_link = get_edit_term_link( $current_object->term_id, $current_object->taxonomy ) )
550 $wp_admin_bar->add_menu( array(
552 'title' => $tax->labels->edit_item,
553 'href' => $edit_term_link
560 * Add "Add New" menu.
564 * @param WP_Admin_Bar $wp_admin_bar
566 function wp_admin_bar_new_content_menu( $wp_admin_bar ) {
569 $cpts = (array) get_post_types( array( 'show_in_admin_bar' => true ), 'objects' );
571 if ( isset( $cpts['post'] ) && current_user_can( $cpts['post']->cap->create_posts ) )
572 $actions[ 'post-new.php' ] = array( $cpts['post']->labels->name_admin_bar, 'new-post' );
574 if ( isset( $cpts['attachment'] ) && current_user_can( 'upload_files' ) )
575 $actions[ 'media-new.php' ] = array( $cpts['attachment']->labels->name_admin_bar, 'new-media' );
577 if ( current_user_can( 'manage_links' ) )
578 $actions[ 'link-add.php' ] = array( _x( 'Link', 'add new from admin bar' ), 'new-link' );
580 if ( isset( $cpts['page'] ) && current_user_can( $cpts['page']->cap->create_posts ) )
581 $actions[ 'post-new.php?post_type=page' ] = array( $cpts['page']->labels->name_admin_bar, 'new-page' );
583 unset( $cpts['post'], $cpts['page'], $cpts['attachment'] );
585 // Add any additional custom post types.
586 foreach ( $cpts as $cpt ) {
587 if ( ! current_user_can( $cpt->cap->create_posts ) )
590 $key = 'post-new.php?post_type=' . $cpt->name;
591 $actions[ $key ] = array( $cpt->labels->name_admin_bar, 'new-' . $cpt->name );
593 // Avoid clash with parent node and a 'content' post type.
594 if ( isset( $actions['post-new.php?post_type=content'] ) )
595 $actions['post-new.php?post_type=content'][1] = 'add-new-content';
597 if ( current_user_can( 'create_users' ) || current_user_can( 'promote_users' ) )
598 $actions[ 'user-new.php' ] = array( _x( 'User', 'add new from admin bar' ), 'new-user' );
603 $title = '<span class="ab-icon"></span><span class="ab-label">' . _x( 'New', 'admin bar menu group label' ) . '</span>';
605 $wp_admin_bar->add_menu( array(
606 'id' => 'new-content',
608 'href' => admin_url( current( array_keys( $actions ) ) ),
611 foreach ( $actions as $link => $action ) {
612 list( $title, $id ) = $action;
614 $wp_admin_bar->add_menu( array(
615 'parent' => 'new-content',
618 'href' => admin_url( $link )
624 * Add edit comments link with awaiting moderation count bubble.
628 * @param WP_Admin_Bar $wp_admin_bar
630 function wp_admin_bar_comments_menu( $wp_admin_bar ) {
631 if ( !current_user_can('edit_posts') )
634 $awaiting_mod = wp_count_comments();
635 $awaiting_mod = $awaiting_mod->moderated;
636 $awaiting_title = esc_attr( sprintf( _n( '%s comment awaiting moderation', '%s comments awaiting moderation', $awaiting_mod ), number_format_i18n( $awaiting_mod ) ) );
638 $icon = '<span class="ab-icon"></span>';
639 $title = '<span id="ab-awaiting-mod" class="ab-label awaiting-mod pending-count count-' . $awaiting_mod . '">' . number_format_i18n( $awaiting_mod ) . '</span>';
641 $wp_admin_bar->add_menu( array(
643 'title' => $icon . $title,
644 'href' => admin_url('edit-comments.php'),
645 'meta' => array( 'title' => $awaiting_title ),
650 * Add appearance submenu items to the "Site Name" menu.
654 * @param WP_Admin_Bar $wp_admin_bar
656 function wp_admin_bar_appearance_menu( $wp_admin_bar ) {
657 $wp_admin_bar->add_group( array( 'parent' => 'site-name', 'id' => 'appearance' ) );
659 $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
660 $customize_url = add_query_arg( 'url', urlencode( $current_url ), wp_customize_url() );
662 if ( current_user_can( 'switch_themes' ) ) {
663 $wp_admin_bar->add_menu( array(
664 'parent' => 'appearance',
666 'title' => __( 'Themes' ),
667 'href' => admin_url( 'themes.php' ),
669 'class' => 'hide-if-customize',
673 if ( current_user_can( 'customize' ) ) {
674 $wp_admin_bar->add_menu( array(
675 'parent' => 'appearance',
676 'id' => 'customize-themes',
677 'title' => __( 'Themes' ),
678 'href' => add_query_arg( urlencode( 'autofocus[section]' ), 'themes', $customize_url ), // urlencode() needed due to #16859
680 'class' => 'hide-if-no-customize',
686 if ( current_user_can( 'customize' ) ) {
687 $wp_admin_bar->add_menu( array(
688 'parent' => 'appearance',
690 'title' => __('Customize'),
691 'href' => $customize_url,
693 'class' => 'hide-if-no-customize',
696 add_action( 'wp_before_admin_bar_render', 'wp_customize_support_script' );
699 if ( ! current_user_can( 'edit_theme_options' ) ) {
703 if ( current_theme_supports( 'widgets' ) ) {
704 $wp_admin_bar->add_menu( array(
705 'parent' => 'appearance',
707 'title' => __( 'Widgets' ),
708 'href' => admin_url( 'widgets.php' ),
710 'class' => 'hide-if-customize',
714 if ( current_user_can( 'customize' ) ) {
715 $wp_admin_bar->add_menu( array(
716 'parent' => 'appearance',
717 'id' => 'customize-widgets',
718 'title' => __( 'Widgets' ),
719 'href' => add_query_arg( urlencode( 'autofocus[panel]' ), 'widgets', $customize_url ), // urlencode() needed due to #16859
721 'class' => 'hide-if-no-customize',
727 if ( current_theme_supports( 'menus' ) || current_theme_supports( 'widgets' ) )
728 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'menus', 'title' => __('Menus'), 'href' => admin_url('nav-menus.php') ) );
730 if ( current_theme_supports( 'custom-background' ) ) {
731 $wp_admin_bar->add_menu( array(
732 'parent' => 'appearance',
733 'id' => 'background',
734 'title' => __( 'Background' ),
735 'href' => admin_url( 'themes.php?page=custom-background' ),
737 'class' => 'hide-if-customize',
741 if ( current_user_can( 'customize' ) ) {
742 $wp_admin_bar->add_menu( array(
743 'parent' => 'appearance',
744 'id' => 'customize-background',
745 'title' => __( 'Background' ),
746 'href' => add_query_arg( urlencode( 'autofocus[control]' ), 'background_image', $customize_url ), // urlencode() needed due to #16859
748 'class' => 'hide-if-no-customize',
754 if ( current_theme_supports( 'custom-header' ) ) {
755 $wp_admin_bar->add_menu( array(
756 'parent' => 'appearance',
758 'title' => __( 'Header' ),
759 'href' => admin_url( 'themes.php?page=custom-header' ),
761 'class' => 'hide-if-customize',
765 if ( current_user_can( 'customize' ) ) {
766 $wp_admin_bar->add_menu( array(
767 'parent' => 'appearance',
768 'id' => 'customize-header',
769 'title' => __( 'Header' ),
770 'href' => add_query_arg( urlencode( 'autofocus[control]' ), 'header_image', $customize_url ), // urlencode() needed due to #16859
772 'class' => 'hide-if-no-customize',
781 * Provide an update link if theme/plugin/core updates are available.
785 * @param WP_Admin_Bar $wp_admin_bar
787 function wp_admin_bar_updates_menu( $wp_admin_bar ) {
789 $update_data = wp_get_update_data();
791 if ( !$update_data['counts']['total'] )
794 $title = '<span class="ab-icon"></span><span class="ab-label">' . number_format_i18n( $update_data['counts']['total'] ) . '</span>';
795 $title .= '<span class="screen-reader-text">' . $update_data['title'] . '</span>';
797 $wp_admin_bar->add_menu( array(
800 'href' => network_admin_url( 'update-core.php' ),
802 'title' => $update_data['title'],
812 * @param WP_Admin_Bar $wp_admin_bar
814 function wp_admin_bar_search_menu( $wp_admin_bar ) {
818 $form = '<form action="' . esc_url( home_url( '/' ) ) . '" method="get" id="adminbarsearch">';
819 $form .= '<input class="adminbar-input" name="s" id="adminbar-search" type="text" value="" maxlength="150" />';
820 $form .= '<label for="adminbar-search" class="screen-reader-text">' . __( 'Search' ) . '</label>';
821 $form .= '<input type="submit" class="adminbar-button" value="' . __('Search') . '"/>';
824 $wp_admin_bar->add_menu( array(
825 'parent' => 'top-secondary',
829 'class' => 'admin-bar-search',
836 * Add secondary menus.
840 * @param WP_Admin_Bar $wp_admin_bar
842 function wp_admin_bar_add_secondary_groups( $wp_admin_bar ) {
843 $wp_admin_bar->add_group( array(
844 'id' => 'top-secondary',
846 'class' => 'ab-top-secondary',
850 $wp_admin_bar->add_group( array(
851 'parent' => 'wp-logo',
852 'id' => 'wp-logo-external',
854 'class' => 'ab-sub-secondary',
860 * Style and scripts for the admin bar.
864 function wp_admin_bar_header() { ?>
865 <style type="text/css" media="print">#wpadminbar { display:none; }</style>
870 * Default admin bar callback.
874 function _admin_bar_bump_cb() { ?>
875 <style type="text/css" media="screen">
876 html { margin-top: 32px !important; }
877 * html body { margin-top: 32px !important; }
878 @media screen and ( max-width: 782px ) {
879 html { margin-top: 46px !important; }
880 * html body { margin-top: 46px !important; }
887 * Set the display status of the admin bar.
889 * This can be called immediately upon plugin load. It does not need to be called from a function hooked to the init action.
893 * @param bool $show Whether to allow the admin bar to show.
896 function show_admin_bar( $show ) {
897 global $show_admin_bar;
898 $show_admin_bar = (bool) $show;
902 * Determine whether the admin bar should be showing.
906 * @return bool Whether the admin bar should be showing.
908 function is_admin_bar_showing() {
909 global $show_admin_bar, $pagenow;
911 // For all these types of requests, we never want an admin bar.
912 if ( defined('XMLRPC_REQUEST') || defined('DOING_AJAX') || defined('IFRAME_REQUEST') )
915 // Integrated into the admin.
919 if ( ! isset( $show_admin_bar ) ) {
920 if ( ! is_user_logged_in() || 'wp-login.php' == $pagenow ) {
921 $show_admin_bar = false;
923 $show_admin_bar = _get_admin_bar_pref();
928 * Filter whether to show the admin bar.
930 * Returning false to this hook is the recommended way to hide the admin bar.
931 * The user's display preference is used for logged in users.
935 * @param bool $show_admin_bar Whether the admin bar should be shown. Default false.
937 $show_admin_bar = apply_filters( 'show_admin_bar', $show_admin_bar );
939 return $show_admin_bar;
943 * Retrieve the admin bar display preference of a user.
948 * @param string $context Context of this preference check. Defaults to 'front'. The 'admin'
949 * preference is no longer used.
950 * @param int $user Optional. ID of the user to check, defaults to 0 for current user.
951 * @return bool Whether the admin bar should be showing for this user.
953 function _get_admin_bar_pref( $context = 'front', $user = 0 ) {
954 $pref = get_user_option( "show_admin_bar_{$context}", $user );
955 if ( false === $pref )
958 return 'true' === $pref;