Tags

, ,

sites

If you are running a multisite set up, then you get a sites listing page showing all the sites on that network. Sometimes there may be need to show some more information. So let’s discuss how we can achieve this through some custom coding. You can put the custom code inside any plugin or in the active theme’s functions.php file.

We will add a column for showing the site id for each of the sites. For this, we will take the help of wpmu_blogs_columns hook. This takes the columns list as a parameter and we will manipulate to include another column. Have a look at the code below.

// Hook to columns on network sites listing
add_filter( 'wpmu_blogs_columns', 'mfs_blogs_columns' );

/**
* To add a columns to the sites columns
*
* @param array
*
* @return array
*/
function mfs_blogs_columns($sites_columns)
{
	$columns_1 = array_slice( $sites_columns, 0, 1 );
	$columns_2 = array_slice( $sites_columns, 1 );
	
	$sites_columns = $columns_1 + array( 'blogid' => 'Site ID' ) + $columns_2;
	
	return $sites_columns;
}

Here we have sliced the columns array into 2 pieces. This is because we want to place the new column at a custom position (2nd place). If we simply add the column to the array, then it will come at the last.

Now we have a column for holding the site ids. Next thing we will be doing is to show the site id for each of the sites. This time we will use another hook manage_sites_custom_column. Here we will be checking for the column name. If it matches with the custom column, then we will print the value for that column.

// Hook to manage column data on network sites listing
add_action( 'manage_sites_custom_column', 'mfs_sites_custom_column', 10, 2 );

/**
* Show blog id
*
* @param string
* @param integer
*
* @return void
*/
function mfs_sites_custom_column($column_name, $blog_id)
{
	if ( $column_name == 'blogid' ) {
		echo $blog_id;
	}
}

As you can see above, we have printed the blog id when the column matched with the blog id column. That’s it. Now you can see a new column on the second position showing the site/blog id.

Advertisements