Display a list of subsites the logged in user is able to access

Hi. Is it possible to display a list of subsites the logged in user is able to access?? My users all have access to the main site, and then they'll need to access there own subsite through the main site, so if it's possible to create a site on the main site, that shows all the subsites the logged in user is able to access i would be thrilled :smiley: Hope you can help

Eg. User 1
- access to mainsite.com and number1.mainsite.com
User 2
- access to mainsite.com and number2.mainsite.com

On the main site, when user 1 is logged in i would like to show 2 links:
http://www.mainsite.com
http://www.number1.mainsite.com

And of course when user 2 is logged in i would like to show these 2 links:
http://www.mainsite.com
http://www.number2.mainsite.com

Really hope you can help me :slight_smile:

  • Rupok
    • Support Ninja

    Hi Danni,

    Thanks for asking. This can be done by creating a custom shortcode with get_blogs_of_user( ) function. To make things even easier for you, I've written the code for you. You will need to use this code in a MU Plugin. For creating a MU Plugin, create a folder named "mu-plugins" inside your "wp-content" folder. Then inside that "mu-plugins" folder, create a file with any name and with a .php extension. Now paste the code inside that file and save.

    <?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_id = 1;
    	$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' );

    For getting more idea on mu plugins, you can go through this article: http://premium.wpmudev.org/manuals/wpmu-manual-2/using-mu-plugins/

    After doing this, create a page in your network main site and paste the following shortcode in the content area of that page:
    [bloglist]
    That will show the linked list of all the sites the logged in user belongs to.

    Please let us know if this doesn't work or if you have any confusion. We will be glad to help further.

    Have a nice day. Cheers!
    Rupok

    • Danni
      • WPMU DEV Initiate

      Hi again.
      Just realized that the code shows all sites and not only the ones a specific user is registered to.

      I have a user called “NewUser” whom is not registered on the site template.penthousecph.dk.
      Still it shows on the page. Could you please help me rewrite the code, so that it’s only the sites a specific user is registered on that shows on the list, and not all the sites in the multisite setup??

  • Kasia Swiderska
    • Support nomad

    Hello Danni,

    There is small error in that code. Please try this one:

    <?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' );

    kind regards,
    Kasia

  • Kasia Swiderska
    • Support nomad

    Hello Danni,

    Please try this code:

    <?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) {
    
    		if ( 1 != $user_blog->userblog_id) {
    			 echo '<li><a href="'.$user_blog->siteurl.'">'.$user_blog->blogname.'</a></li>';
    
    		}
    
    	}
    	echo '</ul>';
    }
    }
    add_shortcode( 'bloglist', 'show_blog_list' );

    kind regards,
    Kasia

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.