WordPress Multisite: Adding the current site ID to the admin bar

I’ve worked with some large multisite installs; one common issue I run into is identifying the ID for the site I’m looking at.

There are a couple of ways to lookup the ID of the current site:

    • Go to Network Admin and look for the site you’re on
    • Look in the wp_blogs table and search for the URL for the site you’re on

Both of these methods work, but take time and are clunky and take a little time to complete.

I’ve put together a simple plugin, also available on Github Gist, that looks up the current site ID and adds a link to the Admin Bar with the ID and a link to network admin:

 *	Plugin Name: Admin Bar Site ID
 *	Description: Add the current site ID to the Admin Bar
 *	Version:     1.0.3
 *	Author:      James Morrison
 *	Author URI:  https://www.jamesmorrison.me/

 * Security Check
 * @since 1.0.0

defined( 'ABSPATH' ) or die();

 * Add Site ID to admin bar
 * @since 1.0.0

add_action( 'admin_bar_menu',
	function( $wp_admin_bar ) {
		// Sanity check - this won't work if this isn't a multisite or if the user can't access network admin
		if ( ! is_multisite() || ! current_user_can( 'manage_network' ) ) {
		// Get the Site ID
		$_site_id = get_current_blog_id();
		// Add the Node
		$wp_admin_bar->add_node( [
			"id"    => "site-id",
			"title" => "Site ID: $_site_id"
			"href"  => esc_url( add_query_arg( 'id', $_site_id, network_admin_url( 'site-info.php' ) ) ),
		] );

999, 1 );