* Toolbar API: Top-level Toolbar functionality
* Instantiates 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. This also gives you access to the `$post` global,
* @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 ) ) {
* Loads all necessary admin bar items.
* This hook can add, remove, or manipulate admin bar items. The priority
* determines the placement for new items, and changes to existing items
* would require a high priority. To remove or manipulate existing nodes
* without a specific priority, use `wp_before_admin_bar_render`.
* @param WP_Admin_Bar $wp_admin_bar The 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' );
* Adds the WordPress logo menu.
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance.
function wp_admin_bar_wp_menu( $wp_admin_bar ) {
if ( current_user_can( 'read' ) ) {
$about_url = self_admin_url( 'about.php' );
$contribute_url = self_admin_url( 'contribute.php' );
} elseif ( is_multisite() ) {
$about_url = get_dashboard_url( get_current_user_id(), 'about.php' );
$contribute_url = get_dashboard_url( get_current_user_id(), 'contribute.php' );
$wp_logo_menu_args = array(
'title' => '<span class="ab-icon" aria-hidden="true"></span><span class="screen-reader-text">' .
/* translators: Hidden accessibility text. */
__( 'About WordPress' ) .
'menu_title' => __( 'About WordPress' ),
// 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' ),
'title' => __( 'Get Involved' ),
'href' => $contribute_url,
// 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/documentation/' ),
'parent' => 'wp-logo-external',
'title' => __( 'Learn WordPress' ),
'href' => __( 'https://learn.wordpress.org/' ),
'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' ),
* Adds the sidebar toggle button.
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance.
function wp_admin_bar_sidebar_toggle( $wp_admin_bar ) {
'title' => '<span class="ab-icon" aria-hidden="true"></span><span class="screen-reader-text">' .
/* translators: Hidden accessibility text. */
* Adds the "My Account" item.
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance.
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,
/* translators: %s: Current user's display name. */
'menu_title' => sprintf( __( 'Howdy, %s' ), $current_user->display_name ),
'tabindex' => ( false !== $profile_url ) ? '' : 0,
* Adds the "My Account" submenu items.
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance.
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>";
if ( false !== $profile_url ) {
$user_info .= "<span class='display-name edit-profile'>" . __( 'Edit Profile' ) . '</span>';
'parent' => 'user-actions',
'parent' => 'user-actions',
'title' => __( 'Log Out' ),
'href' => wp_logout_url(),
* Adds the "Site Name" menu.
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance.
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' => __( 'Manage 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 );
if ( current_user_can( 'activate_plugins' ) ) {
'title' => __( 'Plugins' ),
'href' => admin_url( 'plugins.php' ),
* Adds the "Edit Site" link to the Toolbar.
* @since 6.3.0 Added `$_wp_current_template_id` global for editing of current template directly from the admin bar.
* @since 6.6.0 Added the `canvas` query arg to the Site Editor link.
* @global string $_wp_current_template_id
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance.
function wp_admin_bar_edit_site_menu( $wp_admin_bar ) {
global $_wp_current_template_id;
// Don't show if a block theme is not activated.
if ( ! wp_is_block_theme() ) {
// Don't show for users who can't edit theme options or when in the admin.
if ( ! current_user_can( 'edit_theme_options' ) || is_admin() ) {
'title' => __( 'Edit Site' ),
'postType' => 'wp_template',
'postId' => $_wp_current_template_id,
admin_url( 'site-editor.php' )
* Adds the "Customize" link to the Toolbar.
* @global WP_Customize_Manager $wp_customize