I’m trying to get the primary blog URL of a user. What I have works to grab the details, but if there is more than one blog it just returns them all 🙁

$user_id = get_current_user_id();
$user_blogs = get_blogs_of_user( $user_id );
foreach ($user_blogs AS $user_blog) {
 echo $user_blog->siteurl;
}

How can I modify this to output the primary blog URL of the current user?

I’m not sure how to pull it from wp_usermeta primary_blog table, but that may do the trick.


What I would like is the URL from the user primary blog so when they login from the network main site and visit the account area, it shows links to various areas without directing them to the you attempted to access message first…

1 Answer
1

There’s actually a function for that: get_active_blog_for_user() https://developer.wordpress.org/reference/functions/get_active_blog_for_user/

$user_id = get_current_user_id();
$user_blog = get_active_blog_for_user( $user_id );
echo $user_blog->siteurl;

Leave a Reply

Your email address will not be published. Required fields are marked *