Creating a dynamic link to "home" for site admins

Hi,

I wanted to create a link for subsite admins that would redirect them to their individual home pages... link to be created on a static front end page.

I followed some of the steps from this post, but instead of using a template, I created shortcode and added it to a specific page.

https://premium.wpmudev.org/forums/topic/how-do-i-create-a-dynamic-home-page-link

However, the code doesn't seem to work.. nothing actually appears on the page. I added he following to my childs function.php, but nothing. I added another simple shortcode that I created to display hello world, and that works fine. So i'm not sure where the problem is... i suspect that the blogid is not being retrieved properly?? Is there anything I can add to this snippet of code that might help me figure out what its doing or not doing?

here's the code snippet:

function showhomeshortcode() {

	global $user;
	$blogs = get_blogs_of_user( $user->ID, true );
	if ( is_array( $blogs ) ) {
		foreach ( (array) $blogs as $key => $val ) {
			if( $val->userblog_id != 1 ) {
				echo ' <a class="' . $class . '" href="' . esc_url( get_home_url( $val->userblog_id ) ) . '">' . __( 'Home' ) . '</a> ';
				break;
			}
		}
	}

}
add_shortcode('showhome', 'showhomeshortcode');

thanks!
Shayne

  • Predrag Dubajic
    • Support

    Hey Shayne,

    Hope you're doing well today :slight_smile:

    Can you give this code a try and see if that does the trick for you:

    function showhomeshortcode() {
    
    	$user_id = get_current_user_id();
    	$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('showhome', 'showhomeshortcode');

    Let us know how it goes :slight_smile:

    Best regards,
    Predrag

  • Shayne
    • Site Builder, Child of Zeus

    Hey Predrag,

    Thanks for this... your new code snippet seems to work however, I was hoping to only display the users blog and not both the user blog and mainsite. Any ideas how i could go about modifying the code you provide to omit the mainsite?

    thanks!
    Shayne

  • Adam Czajczyk
    • Support Gorilla

    Hello Shayne!

    Try this code instead please:

    function showhomeshortcode() {
    
    	$user_id = get_current_user_id();
    	$user_blogs = get_blogs_of_user( $user_id );
    	foreach ($user_blogs AS $user_blog) {
    		if ($user_blog->userblog_id > 1)
    			echo '<li><a href="'.$user_blog->siteurl.'">'.$user_blog->userblog_id.'</a></li>';
    	}
    	echo '</ul>';
    
    }
    add_shortcode('showhome', 'showhomeshortcode');

    There's an "if" condition added that skips the site of ID = 1, which by default is the main site. If you changed the main site via wp-config.php file, update the value to the real ID of your main site.

    I hope that helps!

    Best regards,
    Adam

  • Shayne
    • Site Builder, Child of Zeus

    Excellent Adam! That worked perfectly.

    I modified it a little because the output was loading before the content... I'm not sure if I did this right, but it seems to work so far.

    function showhomeshortcode() {
    
    	$return = '';
    	$user_id = get_current_user_id();
    	$user_blogs = get_blogs_of_user( $user_id );
    	foreach ($user_blogs AS $user_blog) {
    		if ($user_blog->userblog_id > 1)
    			$return .= '<a href="'.$user_blog->siteurl.'" target="_blank">'.$user_blog->siteurl.'</a>';
    	}
    	return $return;
    }
    add_shortcode('showhome', 'showhomeshortcode');

    Thanks again!
    Shayne

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.