函数文档

wp_admin_bar_site_menu()

💡 云策文档标注

概述

wp_admin_bar_site_menu() 是 WordPress 核心函数,用于在管理工具栏中添加“站点名称”菜单及其子菜单项。该函数根据用户权限和当前环境动态生成菜单内容,包括站点名称、访问链接和管理选项。

关键要点

  • 函数参数:接受一个 WP_Admin_Bar 实例作为必需参数。
  • 访问控制:仅对已登录用户显示,且用户必须是站点成员或超级管理员。
  • 菜单生成:基于站点名称、网络管理或用户管理状态设置菜单标题,并添加相应的子菜单项。
  • 条件逻辑:根据 is_admin()、current_user_can() 等条件判断,在前端或后端显示不同的子菜单。
  • 相关函数:涉及 get_bloginfo()、wp_html_excerpt()、WP_Admin_Bar::add_node() 等多个核心函数。

代码示例

add_action( 'admin_bar_menu', function ( $wp_admin_bar ) {
    // 移除默认链接
    $wp_admin_bar->remove_node( 'view-site' );

    // 添加自定义链接到 example.com
    $wp_admin_bar->add_node( array(
        'parent' => 'site-name',
        'id'     => 'view-example',
        'title'  => 'Go to example.com',
        'href'   => 'https://www.example.com'
    ) );

    // 重新添加主页链接
    $wp_admin_bar->add_node( array(
        'parent' => 'site-name',
        'id'     => 'view-home',
        'title'  => 'Go to homepage',
        'href'   => home_url( '/' )
    ) );
}, 999 ); // 注意优先级 999,确保能移除默认链接

注意事项

  • 函数在 WordPress 3.3.0 版本中引入。
  • 用户贡献笔记提供了自定义菜单链接的示例,需注意优先级设置以确保正确移除默认项。

📄 原文内容

Adds the “Site Name” menu.

Parameters

$wp_admin_barWP_Admin_Barrequired
The WP_Admin_Bar instance.

Source

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(),
			'meta'  => array(
				'menu_title' => $title,
			),
		)
	);

	// 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'  => __( '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.
		$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' ),
				)
			);
		}
	}
}

Changelog

Version Description
3.3.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    You can add more links to the ‘View site’ menu. This example will remove the default link, re-add it with a different name and also add another link to example.com:

    add_action( 'admin_bar_menu', function ( $wp_admin_bar ) {
        // Remove default link
        $wp_admin_bar->remove_node( 'view-site' );
    
        // example.com
        $wp_admin_bar->add_node( array(
            'parent' => 'site-name',
            'id'     => 'view-example',
            'title'  => 'Go to example.com',
            'href'   => 'https://www.example.com'
        ) );
    
        // default home
        $wp_admin_bar->add_node( array(
            'parent' => 'site-name',
            'id'     => 'view-home',
            'title'  => 'Go to homepage',
            'href'   => home_url( '/' )
        ) );
    }, 999 ); // Note the 999, this is needed to make sure we can actually remove the default link