WordPress.org

Ready to get started?Download WordPress

Forums

HOW TO GET THE URL/PATH OF CURRENT USERS SITE (6 posts)

  1. MYGM
    Member
    Posted 1 year ago #

    Hi, I am working on a multi-site which I have setup with sub-directories and not sub-domains. I am trying to create a link that will take the current logged in user to their site url with a page name added to the end.

    For example, the user will login and do everything on the main site but then there is a page that will be the same for everyone, just different prefix before the "/" --- Im not sure if I worded that correctly. Here is what I have, maybe it'll explain a little better -- <a href="<?php $blog = get_active_blog_for_user( get_current_user_id() ); $site_url = $blog->path ?>/upload-document">Upload Documents</a>

    So if the user site is example.com/user, then I would like to be able to dynamically echo their site path with "/upload-document" added to the end.

    How can I correctly do so? Any help is appreciated. Thanks.

  2. Please don't post titles in all caps.

    What's not happening in your code? What URL are you getting out of that?

  3. MYGM
    Member
    Posted 1 year ago #

    Um ok.

    Im getting the regular url to main blog. No changes.

  4. Looking at some older code to list all the sites someone is a member of:

    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>';
    		}
         }
    }

    I would think that you could fork that and just check if $blog is site 1, skip, otherwise echo...

  5. MYGM
    Member
    Posted 1 year ago #

    Thanks for your time, however I can not get it to work.

    The multisite is using sub-directories, I need it to pull the users blog link. ie - domain.com/MYGM/upload-document

    Also How can I do it without the echo, something like my previous example <a href="<?php $blog = get_active_blog_for_user( get_current_user_id() ); $site_url = $blog->path ?>/upload-document">Upload Documents</a>

  6. MYGM
    Member
    Posted 1 year ago #

    Ok Im almost there. Using the code you provided I was able to retrieve the blog url however it is echoing the url for both the main blog and the users blog. How can I make it so that it only echoes the users blog and not the primary/main blog on the network??

    This is how my code currently looks -

    <?php 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 .'upload-document/">My Documents</a></li>';
             echo '<li><a href="http://' . $blog->domain . $blog->path .'upload-document/?ptype=settings&tab=gateways">Settings</a></li>';
    
    		}
         }
    } ?>
    	<?php endif; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic