View Blog ID in WordPress Multisite

When you work quite a bit with WordPress Multisites, sometimes you need the IDs for some functions or Plugins. The easiest way is via the hover effect with your mouse or you use a little code snippet to add a column with the ID in the table view.



The following code belongs into a Plugin and should be placed in the folder mu-plugins.

class Add_Blog_ID {
	
	public static function init() {
		$class = __CLASS__ ;

		if ( empty( $GLOBALS[ $class ] ) )
			$GLOBALS[ $class ] = new $class;
	}
	
	public function __construct() {
		
		add_filter( 'wpmu_blogs_columns', array( $this, 'get_id' ) );
		add_action( 'manage_sites_custom_column', array( $this, 'add_columns' ), 10, 2 );
		add_action( 'manage_blogs_custom_column', array( $this, 'add_columns' ), 10, 2 );
		add_action( 'admin_footer', array( $this, 'add_style' ) );
	}
	
	public function add_columns( $column_name, $blog_id ) {
		
		if ( 'blog_id' === $column_name )
			echo $blog_id;
		
		return $column_name;
	}

	// Add in a column header
	public function get_id( $columns ) {
		
		$columns['blog_id'] = __('ID');
		
		return $columns;
	}
	
	public function add_style() {
		
		echo '<style>#blog_id { width:7%; }</style>';
	}
}
add_action( 'init', array( 'Add_Blog_ID', 'init' ) );

Alternative you can use the follow plugin with this feature and also list the user ID on network view. You can download the plugin in this GIST 3832632.

Comments are closed.

10 comments

  1. Michael Caputo

    Hey there, i'd really like to use this, but I don't understand this line: The following code belongs into a Plugin and should be placed in the folder mu-plugins.

    Please clarify?

  2. Brian Krogsgard

    Hey Michael,

    The mu-plugins folder is one that operates similarly to the plugins folder, but any plugin in it is automatically activated across your network. It stands for "must-use".

    It is not a folder that comes with your install, so you need to manually create it.

    You can read more about it on Justin Tadlock's article

  3. Frank

    @brian: thanks for this great comment to Michaels questions.

  4. Brian Krogsgard

    @frank

    No problem : )

    And nice snippet. Bookmarked. I bet I'll be needing this in an upcoming project.

  5. Michael Caputo

    Thanks for following up. Just a FYI, i didn't recieve a notification that you answered my question even thought I clicked the "Notify me of followup comments" button on the form.. It may have gone to my spam, but just so you know :D

  6. sebastien

    i don't understand... there is already a colum with ID : http://archiparmentier.com/admin.jpg

  7. Frank

    @sebastian: this is not default; maybe you have active a domain-mapping plugin?

  8. sebastien

    yes franck, that's it, you're right !

2 pingbacks

  1. View Blog ID in WordPress Multisite » The Blog of Blogs
  2. Add Multi Site Blog ID to WordPress - WP Mods