How can I get multisite primary blog (url or path) for current user?

In a multisite setup, when users are visiting a blog/site that there are not a member, I would like to display a link to “HOME” that takes them to their “primary” blog.

I know how to determine if a user is or is not a member of a site with the is_current_blog_user() function. The part I am having a problem with is correctly setting the url/path of the “HOME” link to the current users “primary” blog.

Hypothetical Example:

<a href="https://wordpress.stackexchange.com/questions/46056/<?php this_is_the_path_to_users_primary_blog();?>">HOME</a>

I have found the get_active_blog_for_user (http://codex.wordpress.org/Function_Reference/get_active_blog_for_user) function, and this seems to be a good place to start. But I feel as though I must be missing something, and this must be easier than I am making it.

1 Answer
1

Indeed, get_active_blog_for_user should work.

$blog = get_active_blog_for_user( get_current_user_id() );
$blog_url = $blog->domain... /* or $blog->path, together with $blog->siteurl */

Alternatively:

$blog_id = get_active_blog_for_user( get_current_user_id() )->blog_id;
// note: changed "->userblog_id" to "->blog_id" in row above to make it work.
switch_to_blog( $blog_id ); /* switch context */
$home_url = home_url();
restore_current_blog(); /* back */

Leave a Comment