initialize();
$wp_admin_bar->add_menus();
return true;
}
/**
* 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 3.1.0
* @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() {
global $wp_admin_bar;
static $rendered = false;
if ( $rendered ) {
return;
}
if ( ! is_admin_bar_showing() || ! is_object( $wp_admin_bar ) ) {
return;
}
/**
* Loads all necessary admin bar items.
*
* This is the hook used to add, remove, or manipulate admin bar items.
*
* @since 3.1.0
*
* @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.
*
* @since 3.1.0
*/
do_action( 'wp_before_admin_bar_render' );
$wp_admin_bar->render();
/**
* Fires after the admin bar is rendered.
*
* @since 3.1.0
*/
do_action( 'wp_after_admin_bar_render' );
$rendered = true;
}
/**
* Adds the WordPress logo menu.
*
* @since 3.3.0
*
* @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' );
} else {
$about_url = false;
$contribute_url = false;
}
$wp_logo_menu_args = array(
'id' => 'wp-logo',
'title' => '' .
/* translators: Hidden accessibility text. */
__( 'About WordPress' ) .
'',
'href' => $about_url,
);
// Set tabindex="0" to make sub menus accessible when no URL is available.
if ( ! $about_url ) {
$wp_logo_menu_args['meta'] = array(
'tabindex' => 0,
);
}
$wp_admin_bar->add_node( $wp_logo_menu_args );
if ( $about_url ) {
// Add "About WordPress" link.
$wp_admin_bar->add_node(
array(
'parent' => 'wp-logo',
'id' => 'about',
'title' => __( 'About WordPress' ),
'href' => $about_url,
)
);
}
if ( $contribute_url ) {
// Add contribute link.
$wp_admin_bar->add_node(
array(
'parent' => 'wp-logo',
'id' => 'contribute',
'title' => __( 'Get Involved' ),
'href' => $contribute_url,
)
);
}
// Add WordPress.org link.
$wp_admin_bar->add_node(
array(
'parent' => 'wp-logo-external',
'id' => 'wporg',
'title' => __( 'WordPress.org' ),
'href' => __( 'https://wordpress.org/' ),
)
);
// Add documentation link.
$wp_admin_bar->add_node(
array(
'parent' => 'wp-logo-external',
'id' => 'documentation',
'title' => __( 'Documentation' ),
'href' => __( 'https://wordpress.org/documentation/' ),
)
);
// Add learn link.
$wp_admin_bar->add_node(
array(
'parent' => 'wp-logo-external',
'id' => 'learn',
'title' => __( 'Learn WordPress' ),
'href' => 'https://learn.wordpress.org/',
)
);
// Add forums link.
$wp_admin_bar->add_node(
array(
'parent' => 'wp-logo-external',
'id' => 'support-forums',
'title' => __( 'Support' ),
'href' => __( 'https://wordpress.org/support/forums/' ),
)
);
// Add feedback link.
$wp_admin_bar->add_node(
array(
'parent' => 'wp-logo-external',
'id' => 'feedback',
'title' => __( 'Feedback' ),
'href' => __( 'https://wordpress.org/support/forum/requests-and-feedback' ),
)
);
}
/**
* Adds the sidebar toggle button.
*
* @since 3.8.0
*
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance.
*/
function wp_admin_bar_sidebar_toggle( $wp_admin_bar ) {
if ( is_admin() ) {
$wp_admin_bar->add_node(
array(
'id' => 'menu-toggle',
'title' => '' .
/* translators: Hidden accessibility text. */
__( 'Menu' ) .
'',
'href' => '#',
)
);
}
}
/**
* Adds the "My Account" item.
*
* @since 3.3.0
*
* @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 ( ! $user_id ) {
return;
}
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' );
} else {
$profile_url = false;
}
$avatar = get_avatar( $user_id, 26 );
/* translators: %s: Current user's display name. */
$howdy = sprintf( __( 'Howdy, %s' ), '' . $current_user->display_name . '' );
$class = empty( $avatar ) ? '' : 'with-avatar';
$wp_admin_bar->add_node(
array(
'id' => 'my-account',
'parent' => 'top-secondary',
'title' => $howdy . $avatar,
'href' => $profile_url,
'meta' => array(
'class' => $class,
),
)
);
}
/**
* Adds the "My Account" submenu items.
*
* @since 3.1.0
*
* @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 ( ! $user_id ) {
return;
}
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' );
} else {
$profile_url = false;
}
$wp_admin_bar->add_group(
array(
'parent' => 'my-account',
'id' => 'user-actions',
)
);
$user_info = get_avatar( $user_id, 64 );
$user_info .= "{$current_user->display_name}";
if ( $current_user->display_name !== $current_user->user_login ) {
$user_info .= "{$current_user->user_login}";
}
$wp_admin_bar->add_node(
array(
'parent' => 'user-actions',
'id' => 'user-info',
'title' => $user_info,
'href' => $profile_url,
'meta' => array(
'tabindex' => -1,
),
)
);
if ( false !== $profile_url ) {
$wp_admin_bar->add_node(
array(
'parent' => 'user-actions',
'id' => 'edit-profile',
'title' => __( 'Edit Profile' ),
'href' => $profile_url,
)
);
}
$wp_admin_bar->add_node(
array(
'parent' => 'user-actions',
'id' => 'logout',
'title' => __( 'Log Out' ),
'href' => wp_logout_url(),
)
);
}
/**
* Adds the "Site Name" menu.
*
* @since 3.3.0
*
* @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() ) {
return;
}
// 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' ) ) {
return;
}
$blogname = get_bloginfo( 'name' );
if ( ! $blogname ) {
$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, '…' );
$wp_admin_bar->add_node(
array(
'id' => 'site-name',
'title' => $title,
'href' => ( is_admin() || ! current_user_can( 'read' ) ) ? home_url( '/' ) : admin_url(),
)
);
// Create submenu items.
if ( is_admin() ) {
// Add an option to visit the site.
$wp_admin_bar->add_node(
array(
'parent' => 'site-name',
'id' => 'view-site',
'title' => __( 'Visit Site' ),
'href' => home_url( '/' ),
)
);
if ( is_blog_admin() && is_multisite() && current_user_can( 'manage_sites' ) ) {
$wp_admin_bar->add_node(
array(
'parent' => 'site-name',
'id' => 'edit-site',
'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.
$wp_admin_bar->add_node(
array(
'parent' => 'site-name',
'id' => 'dashboard',
'title' => __( 'Dashboard' ),
'href' => admin_url(),
)
);
// Add the appearance submenu items.
wp_admin_bar_appearance_menu( $wp_admin_bar );
// Add a Plugins link.
if ( current_user_can( 'activate_plugins' ) ) {
$wp_admin_bar->add_node(
array(
'parent' => 'site-name',
'id' => 'plugins',
'title' => __( 'Plugins' ),
'href' => admin_url( 'plugins.php' ),
)
);
}
}
}
/**
* Adds the "Edit site" link to the Toolbar.
*
* @since 5.9.0
*
* @global string $_wp_current_template_id
* @since 6.3.0 Added `$_wp_current_template_id` global for editing of current template directly from the admin bar.
*
* @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() ) {
return;
}
// 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() ) {
return;
}
$wp_admin_bar->add_node(
array(
'id' => 'site-editor',
'title' => __( 'Edit site' ),
'href' => add_query_arg(
array(
'postType' => 'wp_template',
'postId' => $_wp_current_template_id,
),
admin_url( 'site-editor.php' )
),
)
);
}
/**
* Adds the "Customize" link to the Toolbar.
*
* @since 4.3.0
*
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance.
* @global WP_Customize_Manager $wp_customize
*/
function wp_admin_bar_customize_menu( $wp_admin_bar ) {
global $wp_customize;
// Don't show if a block theme is activated and no plugins use the customizer.
if ( wp_is_block_theme() && ! has_action( 'customize_register' ) ) {
return;
}
// Don't show for users who can't access the customizer or when in the admin.
if ( ! current_user_can( 'customize' ) || is_admin() ) {
return;
}
// 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() )
) {
return;
}
$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 );
}
$wp_admin_bar->add_node(
array(
'id' => 'customize',
'title' => __( 'Customize' ),
'href' => $customize_url,
'meta' => array(
'class' => 'hide-if-no-customize',
),
)
);
add_action( 'wp_before_admin_bar_render', 'wp_customize_support_script' );
}
/**
* Adds the "My Sites/[Site Name]" menu and all submenus.
*
* @since 3.1.0
*
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance.
*/
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() ) {
return;
}
// 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' ) ) {
return;
}
if ( $wp_admin_bar->user->active_blog ) {
$my_sites_url = get_admin_url( $wp_admin_bar->user->active_blog->blog_id, 'my-sites.php' );
} else {
$my_sites_url = admin_url( 'my-sites.php' );
}
$wp_admin_bar->add_node(
array(
'id' => 'my-sites',
'title' => __( 'My Sites' ),
'href' => $my_sites_url,
)
);
if ( current_user_can( 'manage_network' ) ) {
$wp_admin_bar->add_group(
array(
'parent' => 'my-sites',
'id' => 'my-sites-super-admin',
)
);
$wp_admin_bar->add_node(
array(
'parent' => 'my-sites-super-admin',
'id' => 'network-admin',
'title' => __( 'Network Admin' ),
'href' => network_admin_url(),
)
);
$wp_admin_bar->add_node(
array(
'parent' => 'network-admin',
'id' => 'network-admin-d',
'title' => __( 'Dashboard' ),
'href' => network_admin_url(),
)
);
if ( current_user_can( 'manage_sites' ) ) {
$wp_admin_bar->add_node(
array(
'parent' => 'network-admin',
'id' => 'network-admin-s',
'title' => __( 'Sites' ),
'href' => network_admin_url( 'sites.php' ),
)
);
}
if ( current_user_can( 'manage_network_users' ) ) {
$wp_admin_bar->add_node(
array(
'parent' => 'network-admin',
'id' => 'network-admin-u',
'title' => __( 'Users' ),
'href' => network_admin_url( 'users.php' ),
)
);
}
if ( current_user_can( 'manage_network_themes' ) ) {
$wp_admin_bar->add_node(
array(
'parent' => 'network-admin',
'id' => 'network-admin-t',
'title' => __( 'Themes' ),
'href' => network_admin_url( 'themes.php' ),
)
);
}
if ( current_user_can( 'manage_network_plugins' ) ) {
$wp_admin_bar->add_node(
array(
'parent' => 'network-admin',
'id' => 'network-admin-p',
'title' => __( 'Plugins' ),
'href' => network_admin_url( 'plugins.php' ),
)
);
}
if ( current_user_can( 'manage_network_options' ) ) {
$wp_admin_bar->add_node(
array(
'parent' => 'network-admin',
'id' => 'network-admin-o',
'title' => __( 'Settings' ),
'href' => network_admin_url( 'settings.php' ),
)
);
}
}
// Add site links.
$wp_admin_bar->add_group(
array(
'parent' => 'my-sites',
'id' => 'my-sites-list',
'meta' => array(
'class' => current_user_can( 'manage_network' ) ? 'ab-sub-secondary' : '',
),
)
);
/**
* Filters whether to show the site icons in toolbar.
*
* Returning false to this hook is the recommended way to hide site icons in the toolbar.
* A truthy return may have negative performance impact on large multisites.
*
* @since 6.0.0
*
* @param bool $show_site_icons Whether site icons should be shown in the toolbar. Default true.
*/
$show_site_icons = apply_filters( 'wp_admin_bar_show_site_icons', true );
foreach ( (array) $wp_admin_bar->user->blogs as $blog ) {
switch_to_blog( $blog->userblog_id );
if ( true === $show_site_icons && has_site_icon() ) {
$blavatar = sprintf(
'',
esc_url( get_site_icon_url( 16 ) ),
esc_url( get_site_icon_url( 32 ) ),
( wp_lazy_loading_enabled( 'img', 'site_icon_in_toolbar' ) ? ' loading="lazy"' : '' )
);
} else {
$blavatar = '