Hello! Welcome to my stories. Today I’m gonna write about a little one regarding WordPress Multisite. I guess you already know what WordPress Multisite is but if you don’t,

Multisite is a feature of WordPress 3.0 and later versions that allows multiple virtual sites to share a single WordPress installation. When the multisite feature is activated, the original WordPress site can be converted to support a network of sites.

Source: https://codex.wordpress.org/Glossary#Multisite

When you have a Multisite, you can access all the blogs (subsites) you belong to in your WordPress Dashboard by clicking the “My Sites” menu in the adminbar. But what if you want to show a list of subsites you belong to in the front-end? That would make things easier, right?

So, how this can be done? It’s super easy. It can be done in many ways but in my opinion, if you register a custom shortcode for showing your list of blogs/subsites, that will be the easiest way to show because you can use this in posts, pages and probably everywhere.

WordPress already has an awesome built-in function for fetching the list of blogs/subsites a user belongs to. You will get details of that function here: WordPress Codex

And to know how you can register a custom shortcode, you can check here: WordPress Codex.

Still thinking it’s complex? Don’t worry. Getting used to with these functions may be a little hard for the first time, so I’ve written the whole code for you. You will get the complete code below.

Now for implementing this code, there are two ways. You can put this piece of code in the functions.php file of your theme, or you can create a MU Plugin (Must Use Plugin). If you don’t know what a Must Use Plugin is,

Must-use plugins (a.k.a. mu-plugins) are plugins installed in a special directory inside the content folder and which are automatically enabled on all sites in the installation. Must-use plugins do not show in the default list of plugins on the Plugins page of wp-admin – although they do appear in a special Must-Use section – and cannot be disabled except by removing the plugin file from the must-use directory, which is found in wp-content/mu-plugins by default.

Source: https://codex.wordpress.org/Must_Use_Plugins

I’d prefer using it in a MU Plugin because these code won’t be working if you change your theme or you will need to add this in your theme functions.php file every time you change your theme. But MU Plugins will stay always.

So if you use this in your theme functions.php file, just remove the php initialization tag from the following code.

Code to use

<?php

function show_blog_list() {

	$user_id = get_current_user_id();
	if ($user_id == 0) {
		echo 'You are not logged in.';
	} else {
		echo '<h2>Here is your blog list</h2>';
		$user_blogs = get_blogs_of_user( $user_id );
		foreach ($user_blogs AS $user_blog) {
			echo '<li><a href="'.$user_blog->siteurl.'">'.$user_blog->blogname.'</a></li>';
		}
		echo '</ul>';
	}

}
add_shortcode( 'bloglist', 'show_blog_list' );

Shortcode to use

Add this shortcode to any post/page or other place(s) where you want to show the list of blogs:

[bloglist]

The output will be something like this depending on your theme style declarations:

Oh, it will also detect if you are logged out and tell you that.

It was easy, right? Just a couple of lines and you get an awesome feature on your Multisite.

I hope you liked this. If you have any confusion, or if it doesn’t work for you, please let me know in the comments section.

Cheers!

Posted by Rupok Chowdhury

Tech Support Ninja at WPMU DEV. WordPress enthusiast, Apple fanboy, Game of Thrones Fan, Photographer, Musician and fun-time Blogger.