wp_get_multisites: WP3/WordPress 3: Generating dynamic list of multisites or replacing get_blog_list

(Psst: You're browsing our pared-down mobile site.)

Update: Yes, this is a convoluted title. Sorry ;-). Who knows if people are searching for WP3, WordPress 3, WordPress 3.0, or its unfortunately search-unfriendly title “Thelonious”.

WordPress 3/WP3 is out! Yes! There are tons of new features, tons of new deprecated features and tons of confusion

Here at Social Ink we pride ourselves on WordPress expertise and one of our favorite features of WordPress 3 is the “Multisite” feature, which weaves together WPMU’s multi-blog features with WordPress’ better plugin management, theme management and the like. We’ll be running a feature on ways to use this new Multisite feature, and today we present our first: How to list sites, or how to get a dynamic list of links (with relative path) of all your various sites (similar to wp_list_categories, or as a replacement for the deprecated get_blog_list). Think of it as a kind of wp_list_sites.

Quick note: You should never use deprecated features if you can help it. WP3 actually still has get_blog_list in its bowels, but to rely on it would be dangerous for future updates. Better to use the newer functions WP3 provides. Without future ado, here is our function, wp_get_multisites, which will retrieve a list of all your sites in WordPress 3, with many extra features, explained below the code.

function wp_get_multisites($user = 1,$get_unwanted = false,$echo_it = true,$list_it = true,$ident = false,$class="") {
		//function to return a list of sites of all blogs on WP3 multisite. by yonatan reinberg/social ink - http://social-ink.net
		$output = ''; $myid = ''; 		
		if($list_it) { $output .= '<ul>'; }				
		$allsites = get_blogs_of_user($user, $get_unwanted);
			foreach ($allsites AS $blog) {
					$myid = 'bloglist_' . $blog->blogname;				
					$output .= '<li><a class="'.$class.'" id="'.$myid.'" href="'.$blog->path.'">'.$blog->blogname.'</a></li>';
					$output .= '<a class="'.$class.'" id="'.$myid.'" href="'.$blog->path.'">'.$blog->blogname.'</a>';							
		if($list_it) { $output .= '</ul>'; }
			echo $output;
			return $output;

To begin, put this whole function in the functions.php file of your ACTIVE THEME. Since Multisite has support for various themes, this seems like a no-brainer but it might get you in the end.

Now, on to how to use it. Put the following wherever you want to display a list of your blogs:

<? wp_get_multisites() ?>

That’s all! But wait, there’s many ways to customize it by passing it variables. The first is the user (use “1” for the superadmin, to get all blogs). The second boolean is whether to get all the sites, or just the ones that are published/availale. The third is whether to to echo or to just return a string. The fourth is whether to create an unordered list or just return a string of the links. The fourth is whether you want special ids assigned to the individual links, and the 6th (!) is whether you’d like a class too… Here is is in all its glory:

<? wp_get_multisites(1,true,true,false,false,'testclass');  ?>

Will return

<a class="testclass" id="" href="/">socialinkdev</a><a class="testclass" id="" href="/anothertest/">anothertest!</a>


<? wp_get_multisites(1,true,true,true,true,'mysites'); ?>

Will return

<li><a class="mysites" id="bloglist_socialinkdev" href="/">socialinkdev</a></li>
<li><a class="mysites" id="bloglist_anothertest!" href="/anothertest/">anothertest!</a></li>

Download the whole function here (1k zip). Any questions? Post em in the comments below – this really helped us with web design for nonprofits in New York City.

Social Ink has been in NYC since 2007!