Codeable info

Add Multi Site Blog ID to WordPress

Posted on by in WordPress Tutorials

If you work with the WordPress Multi Site feature, you may understand how useful it can be to have the option of easily finding the ID number of your network sites. Unfortunately, WordPress does not include any simple way of finding the site ID, not without hovering your mouse over the link and inspecting the URL. Well, this function will make your life much easier. Paste the code below into your functions.php and the ID number of each site will now be displayed in a column on the right side of the site browser.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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 '#blog_id { width:7%; }';
	}
}
add_action( 'init', array( 'Add_Blog_ID', 'init' ) );

This trick was originally published by WP Engineer.

If you’re not already used to using the site IDs for achieving different effects, here’s an example of how I’ve used this trick.

For the last five months, I’ve been doing a lot of heavy development for a network of sites that relies on the WP multi site feature. Each site on the network also uses the same theme. Well, because each of the sites is unique in its own way, such as color scheme, layout styles, etc, I needed a way to check which site I was on. The easiest way to do that is first declare the global $blog_id variable, then check it for the current blog.

1
2
3
4
5
6
7
global $blog_id;
 
if($blog_id == 1) {
 // we are on the main blog
} elseif ($blog_id == 2) {
 // we are on a different blog
}

This way you can easily change layout styles or colors depending on which site you are currently on, and you can use the functions above to easily find the appropriate blog id.

Enjoy!

)
Codeable info

Comments (5)

Comment by Flo says:

Thanks. Works perfectly and just what I needed – big time saver.

Comment by Pippin says:

My good friend AJ Clarke posted a simple tutorial about this a week or so ago: http://www.wpexplorer.com/blog/wordpress-theme-options.html

Comment by Kevin says:

I was actually thinking that myself. I’ve been searching around but I can’t find a simple way to do this. Would you know of any useful way of doing this? Or know of any tutorials that show how to create this? Thanks!

Comment by Pippin says:

I’d recommend setting up a theme options page that has options for each of the pieces of text you’d like to replace. Each site will have its own options.

Comment by Kevin says:

Hey, I’m working on a network of sites that also uses the same theme across multiple blogs. One thing I’ve been trying (and Googling!) to do is to figure out how can I use the functions.php in the theme to accomplish replacing text within the theme.

For example, you have Site 1 and Site 2. I’d like the theme to replace text (like the header) based on the blog id. I’ve seen some long examples but when you need it for over 20 sites, I would love to be able to use the theme’s functions.php to achieve this. Do you have any ideas or know of resources that explain how to do that? Thank you in advance!

Codeable info