2

I have a WordPress Multi Site installation and several blogs. On the frontpage (domain.com) I have a login form and when the user is logged in, it shows two links at the moment (Admin Panel and Log out). But I also want to include a link that goes to the current blog site of the user, eg: blog.domain.com.

<li class="showblog"><a href="<?php <some code> ?>">Show blog</a></li>
1

try use this function,,

function get_primary_blog_url($id=null) {
  // is there a user to check?
  global $current_user;
  $user_id = (null != $id ) ? $id : $current_user->ID;

  if ( isset( $user_id ) ) {
    // Get the blogs of this user
    $user_blogs = get_blogs_of_user( $user_id );

    // Get the URL of the blog
    foreach ( $user_blogs AS $user_blog ) {
      if ( $user_blog->userblog_id != BLOG_ID_CURRENT_SITE ) { 
        return $user_blog->siteurl;
      }
    }
    // If user has no blog, return to the homepage.
  } else {
    return home_url();
  }
}
0

The "current blog" in WPMU terms means the blog that is actually shown in the moment. In other words the current blog context of the page that is viewed in that moment or the content of global $blog_id. As in a normal wp install you would use site_url() to retreive the link of that blog. (Just pointing this out so you don't get confused by that wording later.)

I think what you are searching for is the url to the blog which belongs to that user. There is no direct function for this. The best way to get this information is to use get_blogs_of_user() to get a list of blogs a user is attached to. If your setup is configured in a way that it's only possible for a user to have one blog, you can just use the first element from that array.

Simple example:

<?php
    $blogs = get_blogs_of_user( get_current_user_id() );
    $link = get_site_url($blogs[0]->userblog_id);
?>
<li class="showblog"><a href="<?php echo $link; ?>">Show blog</a></li>
  • Yes, i searching for a way to get the URL to the blog which belongs to the user. I've tried your example and it gives me this link: domain.com/Array – lol5433 Nov 19 '13 at 13:55
  • @Cyb3 I updated the code. – s1lv3r Nov 19 '13 at 14:02
  • Sorry, but the user only got to frontpage:, domain.com. – lol5433 Nov 19 '13 at 14:10
  • Then the user has no blogs? Did you try to check the return value of the get_blogs_of_user() using var_dump()? – s1lv3r Nov 19 '13 at 14:12
  • I tried it and it output: string(16) "domain.com" - I even created a new blog and tried with that user and the same result occurred. – lol5433 Nov 19 '13 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.