simply echo the logged in users username

i want to echo the users username instead of tools in the top bar

:

<ul class="topbardropdownmenu">
    <li class="drop"><a href="#" class="menuitemtools"><?php echo elgg_echo("this to username"); ?></a>
    <ul>
    <?php
        foreach($alphamenu as $item) {
            echo "<li><a href=\"{$item->value}\">" . $item->name . "</a></li>";
        }
    ?>
    </ul>
    </li>
</ul>

  • This should work:

    <?php echo echo get_loggedin_user()->username; ?>

  • Thank you Jeff you saved my hear from being pulled out!

  • Hi guys , can you explain me the reason that when I use this function is sho always NULL value ?

  • This thread is very, very old. It's also not clear where zoli6sic6 tried to add this code (or where you are trying to add any code). Additionally, the code above will surely not work stand alone. For example the variable $item is not assigned any value in the code snippet above. The suggestion of Jeff Tilson is also not longer valid for recent versions of Elgg (and the double "echo" is wrong anyway).

    Using

    echo elgg_get_logged_in_user_entity()->username;

    would output the username of the logged in user. Or

    echo elgg_get_logged_in_user_entity()->name;

    the display name of the user.

    But it really depends on where you are adding this code if it will give you the intended result and if you can add this code in a more elegant way, for example using elgg_get_logged_in_user_entity()->name as an argument in elgg_echo():

    echo elgg_echo("welcome_message", array(elgg_get_logged_in_user_entity()->name));

    with the language string

    "welcome_message" => "Hello %s, welcome at the site."

    added to a language file. If you want to add the code within an html view / code, you would also need to include the php code in open and closing <?php ?> tags.

  • Be careful! There's not always a logged in user

    $user = elgg_get_logged_in_user_entity();
    if (!$user) {
        // logged out
    }
    
  • @Steve is right, without that if statement, your site could turn into a blank page.