List all my WP multisite blogs shortcode

Just a little function with a shortcode to list all your sites in a WP Multisite scenario. It gives you a link to the front end and the dashboard. Simple and easy to tweak.

This function uses three WP functions. You can check out the links for more details.<footnote>This is actually kind of a neat pattern to think about automating for the WP posts. I’ll have to think about how to do this. </footnote>

function wpls_list_all_the_sites(){
   if(is_user_logged_in()){//are you logged in?
      $user_id = get_current_user_id();//what's your id?
      $sites = get_blogs_of_user($user_id);//what are your sites?
      $html = '';
      foreach ($sites as $key => $site) {//for each site . . . do this
         // code...
         $title = $site->blogname;
         $url = $site->siteurl;
         $html .= "<li><a href='{$url}'>{$title}</a> - <a href='{$url}/wp-admin/'>dashboard</a></li>";
      return "<ul id='site-list'>{$html}</ul>";//spit it back

add_shortcode( 'list-sites', 'wpls_list_all_the_sites' );//create the shortcode [list-sites]

Leave a Reply