* Toolbar API: Top-level Toolbar functionality
* Instantiate the admin bar object and set it up as a global for access elsewhere.
* UNHOOKING THIS FUNCTION WILL NOT PROPERLY REMOVE THE ADMIN BAR.
* For that, use show_admin_bar(false) or the {@see 'show_admin_bar'} filter.
* @global WP_Admin_Bar $wp_admin_bar
* @return bool Whether the admin bar was successfully initialized.
function _wp_admin_bar_init() {
if ( ! is_admin_bar_showing() ) {
/* Load the admin bar class code ready for instantiation */
require_once ABSPATH . WPINC . '/class-wp-admin-bar.php';
/* Instantiate the admin bar */
* Filters the admin bar class to instantiate.
* @param string $wp_admin_bar_class Admin bar class to use. Default 'WP_Admin_Bar'.
$admin_bar_class = apply_filters( 'wp_admin_bar_class', 'WP_Admin_Bar' );
if ( class_exists( $admin_bar_class ) ) {
$wp_admin_bar = new $admin_bar_class;
$wp_admin_bar->initialize();
$wp_admin_bar->add_menus();
* Renders the admin bar to the page based on the $wp_admin_bar->menu member var.
* This is called very early on the {@see 'wp_body_open'} action so that it will render
* before anything else being added to the page body.
* For backward compatibility with themes not using the 'wp_body_open' action,
* the function is also called late on {@see 'wp_footer'}.
* It includes the {@see 'admin_bar_menu'} action which should be used to hook in and
* add new menus to the admin bar. That way you can be sure that you are adding at most
* optimal point, right before the admin bar is rendered. This also gives you access to
* the `$post` global, among others.
* @since 5.4.0 Called on 'wp_body_open' action first, with 'wp_footer' as a fallback.
* @global WP_Admin_Bar $wp_admin_bar
function wp_admin_bar_render() {
static $rendered = false;
if ( ! is_admin_bar_showing() || ! is_object( $wp_admin_bar ) ) {
* Load all necessary admin bar items.
* This is the hook used to add, remove, or manipulate admin bar items.
* @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance, passed by reference
do_action_ref_array( 'admin_bar_menu', array( &$wp_admin_bar ) );
* Fires before the admin bar is rendered.
do_action( 'wp_before_admin_bar_render' );
* Fires after the admin bar is rendered.
do_action( 'wp_after_admin_bar_render' );
* Add the WordPress logo menu.
* @param WP_Admin_Bar $wp_admin_bar
function wp_admin_bar_wp_menu( $wp_admin_bar ) {
if ( current_user_can( 'read' ) ) {
$about_url = self_admin_url( 'about.php' );
} elseif ( is_multisite() ) {
$about_url = get_dashboard_url( get_current_user_id(), 'about.php' );
$wp_logo_menu_args = array(
'title' => '<span class="ab-icon"></span><span class="screen-reader-text">' . __( 'About WordPress' ) . '</span>',
// Set tabindex="0" to make sub menus accessible when no URL is available.
$wp_logo_menu_args['meta'] = array(
$wp_admin_bar->add_node( $wp_logo_menu_args );
// Add "About WordPress" link.
'title' => __( 'About WordPress' ),
// Add WordPress.org link.
'parent' => 'wp-logo-external',
'title' => __( 'WordPress.org' ),
'href' => __( 'https://wordpress.org/' ),
// Add documentation link.
'parent' => 'wp-logo-external',
'title' => __( 'Documentation' ),
'href' => __( 'https://wordpress.org/support/' ),
'parent' => 'wp-logo-external',
'id' => 'support-forums',
'title' => __( 'Support' ),
'href' => __( 'https://wordpress.org/support/forums/' ),
'parent' => 'wp-logo-external',
'title' => __( 'Feedback' ),
'href' => __( 'https://wordpress.org/support/forum/requests-and-feedback' ),
* Add the sidebar toggle button.
* @param WP_Admin_Bar $wp_admin_bar
function wp_admin_bar_sidebar_toggle( $wp_admin_bar ) {
'title' => '<span class="ab-icon"></span><span class="screen-reader-text">' . __( 'Menu' ) . '</span>',
* Add the "My Account" item.
* @param WP_Admin_Bar $wp_admin_bar
function wp_admin_bar_my_account_item( $wp_admin_bar ) {
$user_id = get_current_user_id();
$current_user = wp_get_current_user();
if ( current_user_can( 'read' ) ) {
$profile_url = get_edit_profile_url( $user_id );
} elseif ( is_multisite() ) {
$profile_url = get_dashboard_url( $user_id, 'profile.php' );
$avatar = get_avatar( $user_id, 26 );
/* translators: %s: Current user's display name. */
$howdy = sprintf( __( 'Howdy, %s' ), '<span class="display-name">' . $current_user->display_name . '</span>' );
$class = empty( $avatar ) ? '' : 'with-avatar';
'parent' => 'top-secondary',
'title' => $howdy . $avatar,
* Add the "My Account" submenu items.
* @param WP_Admin_Bar $wp_admin_bar
function wp_admin_bar_my_account_menu( $wp_admin_bar ) {
$user_id = get_current_user_id();
$current_user = wp_get_current_user();
if ( current_user_can( 'read' ) ) {
$profile_url = get_edit_profile_url( $user_id );
} elseif ( is_multisite() ) {
$profile_url = get_dashboard_url( $user_id, 'profile.php' );
$wp_admin_bar->add_group(
'parent' => 'my-account',
$user_info = get_avatar( $user_id, 64 );
$user_info .= "<span class='display-name'>{$current_user->display_name}</span>";
if ( $current_user->display_name !== $current_user->user_login ) {
$user_info .= "<span class='username'>{$current_user->user_login}</span>";
'parent' => 'user-actions',
if ( false !== $profile_url ) {
'parent' => 'user-actions',
'title' => __( 'Edit Profile' ),
'parent' => 'user-actions',
'title' => __( 'Log Out' ),
'href' => wp_logout_url(),
* Add the "Site Name" menu.
* @param WP_Admin_Bar $wp_admin_bar
function wp_admin_bar_site_menu( $wp_admin_bar ) {
// Don't show for logged out users.
if ( ! is_user_logged_in() ) {
// Show only when the user is a member of this site, or they're a super admin.
if ( ! is_user_member_of_blog() && ! current_user_can( 'manage_network' ) ) {
$blogname = get_bloginfo( 'name' );
$blogname = preg_replace( '#^(https?://)?(www.)?#', '', get_home_url() );
if ( is_network_admin() ) {
/* translators: %s: Site title. */
$blogname = sprintf( __( 'Network Admin: %s' ), esc_html( get_network()->site_name ) );
} elseif ( is_user_admin() ) {
/* translators: %s: Site title. */
$blogname = sprintf( __( 'User Dashboard: %s' ), esc_html( get_network()->site_name ) );
$title = wp_html_excerpt( $blogname, 40, '…' );
'href' => ( is_admin() || ! current_user_can( 'read' ) ) ? home_url( '/' ) : admin_url(),
// Add an option to visit the site.
'title' => __( 'Visit Site' ),
'href' => home_url( '/' ),
if ( is_blog_admin() && is_multisite() && current_user_can( 'manage_sites' ) ) {
'title' => __( 'Edit Site' ),
'href' => network_admin_url( 'site-info.php?id=' . get_current_blog_id() ),
} elseif ( current_user_can( 'read' ) ) {
// We're on the front end, link to the Dashboard.
'title' => __( 'Dashboard' ),
// Add the appearance submenu items.
wp_admin_bar_appearance_menu( $wp_admin_bar );
* Adds the "Customize" link to the Toolbar.
* @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance.
* @global WP_Customize_Manager $wp_customize
function wp_admin_bar_customize_menu( $wp_admin_bar ) {
// Don't show for users who can't access the customizer or when in the admin.
if ( ! current_user_can( 'customize' ) || is_admin() ) {
// Don't show if the user cannot edit a given customize_changeset post currently being previewed.
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() )
$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
if ( is_customize_preview() && $wp_customize->changeset_uuid() ) {
$current_url = remove_query_arg( 'customize_changeset_uuid', $current_url );
$customize_url = add_query_arg( 'url', urlencode( $current_url ), wp_customize_url() );
if ( is_customize_preview() ) {
$customize_url = add_query_arg( array( 'changeset_uuid' => $wp_customize->changeset_uuid() ), $customize_url );
'title' => __( 'Customize' ),
'href' => $customize_url,
'class' => 'hide-if-no-customize',
add_action( 'wp_before_admin_bar_render', 'wp_customize_support_script' );
* Add the "My Sites/[Site Name]" menu and all submenus.
* @param WP_Admin_Bar $wp_admin_bar
function wp_admin_bar_my_sites_menu( $wp_admin_bar ) {
// Don't show for logged out users or single site mode.
if ( ! is_user_logged_in() || ! is_multisite() ) {
// Show only when the user has at least one site, or they're a super admin.
if ( count( $wp_admin_bar->user->blogs ) < 1 && ! current_user_can( 'manage_network' ) ) {
if ( $wp_admin_bar->user->active_blog ) {
$my_sites_url = get_admin_url( $wp_admin_bar->user->active_blog->blog_id, 'my-sites.php' );
$my_sites_url = admin_url( 'my-sites.php' );
'title' => __( 'My Sites' ),
if ( current_user_can( 'manage_network' ) ) {
$wp_admin_bar->add_group(
'id' => 'my-sites-super-admin',
'parent' => 'my-sites-super-admin',