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) {
				if($ident)
					$myid = 'bloglist_' . $blog->blogname;
				if($list_it)
					$output .= '<li><a class="'.$class.'" id="'.$myid.'" href="'.$blog->path.'">'.$blog->blogname.'</a></li>';
				else
					$output .= '<a class="'.$class.'" id="'.$myid.'" href="'.$blog->path.'">'.$blog->blogname.'</a>';
			}
		if($list_it) { $output .= '</ul>'; }
		if($echo_it)
			echo $output;
		else
			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>

Whereas

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

Will return

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

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.

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

  1. Bruce C says:

    Thank you. I needed an alternative for get_blog_list and this works great.

  2. Chris says:

    Excellent work. To further Stan’s question above, it would be useful to be able to have and exclude option where you could exclude any number of blogs from being listed by their ID.

  3. Stan says:

    Is there a way to exclude the main blog? and show only the subblogs so all sub blogs can be displayed on the main blog homepage

  4. Ken says:

    @webdmelb – think u need superadmin as user on every sub blog.

  5. webdmelb says:

    I’ve tried it all out, I only get the admin site returned. Any idea’s?

  6. yoni says:

    zane and all,
    thanks for the compliments and additions. we’ve been very busy and have neglected our plugins, but i’ll update the function to reflect changes.
    thanks again

  7. zane says:

    One thing I did add was providing a unique name/id for the DOM, as your code produces in valid markup if the $myid param isn’t set.

    I added the following code:
    $blog_id = str_replace(‘/’, ‘_’, strtolower($blog->blogname)) . ‘_’ . $blog->userblog_id;

    for me the str_replace is all I need but I can instances where others might need more, let me know if you want more explanation.

    Also maybe consider putting this on github? or some type of version control?

  8. James_H says:

    Is there any way to sort the output by blog name vs. blog id? Other than that, this is exactly what I have been looking for! Thanks.

  9. Andrea_R says:

    You should probably put this function in a file in mu-plugins, not the theme’s function’s file. In a network installation, it just… makes more sense.

    Also, could you test this with a LOT of blogs? Some users have literally hundreds or thousands of sites. If running this shuts stuff down, well… that can’t be good. :)

  10. adam says:

    is they any way you can display all sites on the network not just the sites that have been created within a created site ?

  11. Nicke says:

    Thanks a lot for this great function.

    How do I excluide the first blog, or the main blog, or the blogid 1 ?

  12. utvikl says:

    Nice post, but it’s a bit wrong. You are assuming here that admin is a member of all blogs, which is not always correct. For this to work the admin account need to be added.

  13. Lars Weimar says:

    Thanks Tom, that’s awesome!

  14. Tom Ransom says:

    I have posted an enhancement to WordPress 3+ to add a new function wp_get_sites($args). Works very similar to wp_get_pages().
    The documentation and a file that be downloaded and tossed in a theme functions.php file are in this ticket – http://core.trac.wordpress.org/ticket/14511

  15. Corey Koltz says:

    Ok, well it didn’t post the scripts, but I tried the first example and the last example.

  16. Corey Koltz says:

    As Yoni wrote, I can only see the admins sites and no other users sites are being published. I have tried both of these

  17. yoni says:

    if you read the notes above, you would have seen it. enter user 1 (superadmin) to get all blogs on the network.

  18. Jason says:

    so this pulls a list of all blogs a single user has. not all blogs on the network.
    any idea how you would pull all blogs on the full network?

  19. phil says:

    This is pretty great guys thanks for making it available. I ran into some trouble though and thought this might help someone else. My network sites are published as sub-domains so using the $blog->path doesn’t link correctly to the site. For my website I made a small change and I am using $blog-> siteurl to create the links instead.

  20. Lars says:

    Any help guys? I really need this and your function is the only one I found that does exactly what I’d need it to do! :(

  21. Lars says:

    Hmm…for some reason, when I implement this function and use ’1′ for Super Admin to get all published blogs, it only returns just a single blog (Super Admin). If I switch that number to ’2′, ’3′, etc.. I get the blog of that particular user. Any clue as to why it won’t retrieve an entire list?

  22. Troy D says:

    Thanks. I’m not familiar with the wp_get_archives so I never thought of using that to create an index of all the categories on the site. So I have been looking in the wrong places. I was looking at wp_list_categories and get_categories.
    I will look this up on the WP codex and see if I can figure this out.

    Again, thank you.
    Troy D

  23. admin says:

    Troy-
    I guess that can be done by modifying the function a little bit, you’d have to to insert another whole wp_get_archives loop on lines 12/14 or something.
    yoni

  24. Troy D says:

    That is amazing. Weeks of searching for a method to do this and I happen to find this post.

    Can the same thing be done with Categories? Can an unordered list be shown with all categories from each site as links?

    Thanks again,
    Troy

  25. admin says:

    Thanks Anders!

    Scott: what do you mean a search?

  26. Anders Haig says:

    This is pretty awesome. I saw it in a search result and thought: “I’ve never heard about that function”. Turns out it’s because it didn’t exist yet. But I’m going to be popping it right into a project I’m working. It’s coincidentally exactly what I need.

    Thanks guys!

  27. Scott says:

    is there a way to add a search for the retrieved list?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>