there, How do i get the primary blog url of the current

Hi there,

How do i get the primary blog url of the current logged in user. Am able to get the ID but not the domain url. Eg. $user_info = get_userdata($user_ID);
echo $user_info->primary_blog;

  • Rupok
    • Support Ninja

    Hi Thomas, hope you had a wonderful day.

    You are almost there. You can use get_blog_details() function. It returns general blog information stored in the wp_blogs table.

    So you can replace this:

    $user_info = get_userdata($user_ID);
    echo $user_info->primary_blog;

    With:

    $user_info = get_userdata($user_ID);
    $blog_details = get_blog_details($user_info->primary_blog);
    echo '<a href='.$blog_details->siteurl.'>'.$blog_details->siteurl.'</a>';

    I've tested this and it's working in my test multisite. Please let us know if you face any problem using this or have any confusion. We will be glad to help further.

    Have a nice day. Cheers!
    Rupok

  • Rupok
    • Support Ninja

    Hi Thomas, hope you had a wonderful day.

    Can you try the following code? I've tested this. It also works.

    if(is_user_logged_in()) {
    global $current_user;
      $blogs = get_blogs_of_user( $current_user->id );
         if($blogs) {
         	 foreach ( $blogs as $blog ) {
             echo '<li><a href="http://' . $blog->domain . $blog->path .'">' .  $blog->blogname . '</a></li>';
    		}
         }
    }

    This will list all sites a user belongs to. Please let us know if you face any problem using this or have any confusion. We will be glad to help further.

    Have a nice day. Cheers!
    Rupok

  • Rupok
    • Support Ninja

    Hi Thomas whittaker, hope you had a wonderful day.

    Well, in that case, replace my previous code with the following:

    if ( ! function_exists( 'get_user_blogs_by_role' ) )
    {
    /**
    * Get all blog IDs where the user has the given role.
    *
    * param  int $user_id
    * param  string $role
    * @return array
    */
    function get_user_blogs_by_role( $user_id, $role )
        {
            $out   = array ();
            $regex = '~' . $GLOBALS['wpdb']->base_prefix . '(\d+)_capabilities~';
            $meta  = get_user_meta( $user_id );
    
            if ( ! $meta )
                return array ();
    
            foreach ( $meta as $key => $value )
            {
                if ( preg_match( $regex, $key, $matches ) )
                {
                    $roles = maybe_unserialize( $meta[$key][0] );
    
        // the number is a string
                    if ( isset ( $roles[$role] ) and 1 === (int) $roles[$role] )
                        $out[] = $matches[1];
                }
            }
    
            return $out;
        }
    }
    
    if(is_user_logged_in()) {
        global $current_user;
    
        $blogs = get_user_blogs_by_role( $current_user->id, 'administrator' );
        if($blogs) {
            echo '<ul>';
            foreach ( $blogs as $blog ) {
                echo '<li><a href="'.get_blog_details( $blog )->siteurl.'">'.get_blog_details( $blog )->siteurl.'</a></li>';
            }
            echo '</ul>';
        }
    }

    Please let us know the result. We will be glad to help further if required.

    Have a nice day. Cheers!
    Rupok

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.