The MU forums have moved to WordPress.org

Show the logged in user's link to his/her blog (23 posts)

  1. Rubyducky
    Member
    Posted 17 years ago #

    Hey everyone,
    On my main index blog, I want to show the user logged in's link to his/her blog. I've tried various code snippets, but they only link to the main blog. For example, On the sidebar of my main blog, I want it to say "Welcome NAME, *Link to his/her blog*". I can't seem to get it to link to their blog, the link always links to the main blog. Any help? Thanks!!!

  2. dsader
    Member
    Posted 17 years ago #

    I use snip this in my sidebars:

    <?php
    global $user_level;
    if ( isset($user_level) ) {
    $blogs = get_blogs_of_user($current_user->ID);
    if ( ! empty($blogs) ) foreach ( $blogs as $blog ) {
    echo "
    <li><a>domain . $blog->path . "'>" . $blog->path . "</a></li>
    ";
    }
    }
    ?>

  3. Rubyducky
    Member
    Posted 17 years ago #

    Thanks for that, I tried it, but it didn't seem to work. I got this error:

    Parse error: syntax error, unexpected ''', expecting ',' or ';' in /opt/lampp/htdocs/wp-content/themes/emire-10/sidebar.php on line 82

    Line 82 is:


    <li><a>domain . $blog->path . "'>" . $blog->path . "</a></li>

    Looks like it doesn't like that line...Any ideas? Thanks!

  4. dsader
    Member
    Posted 17 years ago #

    echo "<li><a href='http://" . $blog->domain . $blog->path . "'>" . $blog->path . "</a></li>";

    When I pasted the first time the code tags borked it.

    (my vhost=no) If you are a vhost=yes then try

    echo "<li><a href='http://" . $blog->domain . $blog->path . "'>" . $blog->domain . $blog->path . "</a></li>";

  5. dsader
    Member
    Posted 17 years ago #

    So the whole bit could look something like this:

    <?php
    global $user_level;
    if ( isset($user_level) ) {
    echo "<h3>Your Blogs</h3>
    ";
    $blogs = get_blogs_of_user($current_user->ID);
    if ( ! empty($blogs) ) foreach ( $blogs as $blog ) {
    echo "<li><a href='http://" . $blog->domain . $blog->path . "'>" . $blog->domain . $blog->path . "</a></li>";
    }
    }
    ?>

  6. Rubyducky
    Member
    Posted 17 years ago #

    Hmm I tried it again, but it didn't work...All I got was "Your Blogs" but nothing under it..:(

  7. eminemjamesuk
    Member
    Posted 17 years ago #

    Yeah I'm wondering how to do this as well. Any help?

  8. zeug
    Member
    Posted 17 years ago #

    I use:

    function home_page_login_form() {
    global $current_user, $blog_id;
    if ( ! is_user_logged_in() ) {
    ?>
    <div id="frontlogin">
    <form name="loginform" id="loginform" action="wp-login.php" method="post">
    <fieldset><legend>Members Login</legend>
    <label>Username:&nbsp;</label><br />
    <input type="text" name="log" id="log" class="textInput" value="" tabindex="1" /><br />
    <label>Password:&nbsp;</label><br />
    <input type="password" name="pwd" class="textInput" value="" tabindex="2" />
    <br /><label><input name="rememberme" type="checkbox" id="rememberme" value="forever" tabindex="3" style="margin:0;" /> Remember me</label>
    <input type="hidden" name="redirect_to" value="/" />
    <br /><input type="submit" name="submit" class="butt" value="Login &raquo;" tabindex="4" />
    </fieldset>
    </form>
    </div>
    <?php
    } else {
    $blogs = get_blogs_of_user($current_user->ID);
    $username = $current_user->user_login;
    $siteurl= get_settings('siteurl');
    ?>
    <div style="margin-left: 10px;">
    <strong>Hi, <?php echo $username; ?>!</strong><br />(<a href="/wp-login.php?action=logout&amp;redirect_to=<?php echo $siteurl; ?>">Logout</a>) <br />
    Your blogs:
    </div>
    <ul>
    <?php
    if ( ! empty($blogs) ) foreach ( $blogs as $blog ) {
    $blog_id = $blog->userblog_id;
    $details = get_blog_details($blog_id);
    $blogname = htmlspecialchars( $details->blogname );
    echo "<li><a href='http://" . $blog->domain . $blog->path . "'>" . $blogname . "</a> | <a href='http://" . $blog->domain . $blog->path . "wp-admin/'>Admin</a></li>";
    }
    ?>
    </ul>
    <?php
    }
    }

    in functions.php then call it from the sidebar.

  9. Rubyducky
    Member
    Posted 17 years ago #

    Thank you for that, but how do I call if from the sidebar?

  10. zeug
    Member
    Posted 17 years ago #

    ummm ... you call the function?

    <?php home_page_login_form(); ?>

  11. Rubyducky
    Member
    Posted 17 years ago #

    Wow thanks! That's exactly what I wanted! Very, very much appreciated :)

  12. zeug
    Member
    Posted 17 years ago #

    No worries, I can't remember where I nicked it from, maybe andrea_r? Use search to trawl these forums, there's a wealth of code snippets to pick through and piece together.

  13. DoggyBloggy
    Member
    Posted 17 years ago #

    Thanks! This is exactly what I was looking for also. One question though. When I login to a blog, it works, but it drops me on the main mu site, rather than the specific blog's main page. I am using vhost=no.

    How can I get the user dropped on their own blog main page after login?

  14. zeug
    Member
    Posted 17 years ago #

    You go through the nightmare of trying to get redirects working properly ... had it going a few versions back using javascript, then it broke, then they introduced multiple blogs per user.

    How does the server decide which blog to redirect to if you belong to more than one?

  15. dsader
    Member
    Posted 17 years ago #

    You got me scratching my head. I use a theme with a left and right sidebar. It(complete code snip from my post above) does not work in the the bar named "sidebar.php" but works in the "leftbar.php".

    Explain that one.

  16. ehab
    Member
    Posted 16 years ago #

    Maybe the sidebar.php doesn't add functions.php ? :D

  17. MazZziKa
    Member
    Posted 16 years ago #

    thx can u widget it please

  18. SebastianCrump
    Member
    Posted 15 years ago #

    Just in case anyone was thinking of putting something similar to zeug's function inline in home.php (as I did) you will need to tweak the code to not change the global $blog_id variable, i.e.

    $thatblog_id = $blog->userblog_id;
    $details = get_blog_details($thatblog_id);

    Otherwise the rest of the page will assume that it's switched to the last blog in the loop.

  19. nitetalker
    Member
    Posted 15 years ago #

    I've succeeded at building a list of links to the current user's blogs as described above.

    What I need help with is building a list of only the blogs for which the current user is an *administrator*. Seems a lot trickier.

    If I can't build that list I'd settle for annotating the applicable list entries in the full list.

  20. nitetalker
    Member
    Posted 15 years ago #

    Answered my own question ...

    To build a list of blogs for which the current user is administrator:


    <?php
    global $current_user, $blog_id;
    $thisblog_id = $blog_id;
    $blogs = get_blogs_of_user($current_user->ID);
    if ( !empty($blogs) ) { ?>
    <p>

      <?php
      foreach ( $blogs as $blog ) {
      $bloggie_id = $blog->userblog_id;
      switch_to_blog($bloggie_id);
      $user_list = get_users_of_blog();
      foreach ( $user_list as $a_user ) {
      $member = new WP_User($a_user->user_id);
      if ($member->has_cap("publish_posts") && $current_user->ID == $a_user->user_id) {
      echo "
    • domain . $blog->path . "'>" . $blog->blogname . "
    • ";
      break;
      }
      }
      }
      switch_to_blog($thisblog_id);
      ?>

    </p>
    <?php } ?>

    I'm not a programmer -- I just cobbled together the solution from a couple of different threads -- so I can't help but think there is a more efficient way to do this -- with a custom database query of some type perhaps.

  21. nitetalker
    Member
    Posted 15 years ago #

    Hoo boy. Took out the unnecessary switching between PHP and HTML and the PHP delimiters and trying again ...


    // global $current_user, $blog_id;
    $thisblog_id = $blog_id;
    $blogs = get_blogs_of_user($current_user->ID);
    if ( !empty($blogs) ) {
    echo '<p>

      ';
      foreach ( $blogs as $blog ) {
      $bloggie_id = $blog->userblog_id;
      switch_to_blog($bloggie_id);
      $user_list = get_users_of_blog();
      foreach ( $user_list as $a_user ) {
      $member = new WP_User($a_user->user_id);
      if ($member->has_cap("publish_posts") && $current_user->ID == $a_user->user_id) {
      echo "
    • domain . $blog->path . "'>" . $blog->blogname . "
    • ";
      break;
      }
      }
      }
      switch_to_blog($thisblog_id);
      echo '
    </p>';
    }

  22. nitetalker
    Member
    Posted 15 years ago #

    [grumble, grumble]
    trying it with backtick characters as code delimiters

    global $current_user, $blog_id;
    $thisblog_id = $blog_id;
    $blogs = get_blogs_of_user($current_user->ID);
    if ( !empty($blogs) ) {
      echo '<p><ul>';
    	foreach ( $blogs as $blog ) {
        $bloggie_id = $blog->userblog_id;
        switch_to_blog($bloggie_id);
        $user_list = get_users_of_blog();
        foreach ( $user_list as $a_user ) {
          $member = new WP_User($a_user->user_id);
          if ($member->has_cap("publish_posts") && $current_user->ID == $a_user->user_id) {
    	      echo "<li><a href='http://" . $blog->domain . $blog->path . "'>" . $blog->blogname . "</a></li>";
    	      break;
          }
        }
      }
    switch_to_blog($thisblog_id);
    echo '</ul></p>';
    }
  23. mdvaldosta
    Member
    Posted 15 years ago #

    The admin bar in the wpmu trac lists all blogs owned when logged in, I'm sure you could snag the code used for that.

About this Topic

  • Started 17 years ago by Rubyducky
  • Latest reply from mdvaldosta