WordPress.org

Ready to get started?Download WordPress

Forums

How do I display "Welcome, (Username)" for logged in users? (7 posts)

  1. RobJonze
    Member
    Posted 7 months ago #

    Is it possible to display "Welcome, (Username)" in the top menu (top-left) of my website when a user is logged in, and display the "My Account" top menu item next to it, BUT have a "Log In / Register" option in its place when there isn't a user logged in?

    Prints-Inc

    I'm a bit new to all this web design stuff, so any help would be much appreciated!

  2. Keith Driscoll
    Member
    Posted 7 months ago #

    Yes, it is possible. However it requires basic knowledge of PHP. Trying to guide you in the right direction, you would need to use a function such as http://codex.wordpress.org/Function_Reference/is_user_logged_in

    Good luck and have fun :)

  3. RobJonze
    Member
    Posted 7 months ago #

    Hey Keith,

    Thanks for the response mate, much appreciated.

    Could you tell me how I go about changing the top menus depending on whether a user was logged in or not?

  4. RobJonze
    Member
    Posted 7 months ago #

    Ok, so I think I might be getting somewhere. Kinda.
    I entered the following code in my functions.php:

    add_filter( 'wp_nav_menu_items', 'my_custom_menu_item');
    function my_custom_menu_item($items)
    {
        if(is_user_logged_in())
        {
            $user=wp_get_current_user();
            $name=$user->display_name; // or user_login , user_firstname, user_lastname
            $items .= '<li style="padding: 1.37em 1em; font-family: bree serif; color: rgb(159, 150, 147);">Welcome, '.$name.'';
        }
        return $items;
    }

    which does indeed display "Welcome, User". However, it displays on both my top menu and my main nav menu.

    What would I need to change to get it to display only on the top menu?

    Thanks in advance.

  5. keesiemeijer
    moderator
    Posted 7 months ago #

    You can test for the menu's theme_location like this:

    add_filter( 'wp_nav_menu_items', 'my_custom_menu_item', 10, 2 );
    function my_custom_menu_item( $items, $args ) {
    
    	if ( isset( $args ) && $args->theme_location === 'primary' ) {
    		// your stuff here
    	}
    	return $items;
    }

    Or use the "wp_nav_menu_{$menu->slug}_items" filter hook. Example with 'top-menu' menu slug.

    add_filter( 'wp_nav_menu_top-menu_items', 'my_custom_menu_item', 10, 2 );
    function my_custom_menu_item( $items, $args ) {
    	// your stuff here
    	return $items;
    }
  6. joergwigger
    Member
    Posted 3 weeks ago #

    hi there ... behind whats following; I AM A ROOKIE!

    i tried to get displayed "Welcome Firstname Lastname". Firstname (in this case ("Mr" "Title") is OK but "Lastname" has a "more" tag and klicking on this is displaying the "Lastname" ...

    +++++++++++++++++++++++++

    add_filter( 'wp_nav_menu_items', 'my_custom_menu_item');
    function my_custom_menu_item($items)
    {
    if(is_user_logged_in())
    {
    $user=wp_get_current_user();
    $name=$user->user_firstname; // or user_login , user_firstname, user_lastname, display_name
    $name2=$user->user_lastname; // or user_login , user_firstname, user_lastname, display_name
    $items .= '<li style="padding: 1.37em 1em; font-family: inherit; color: #FFAD00;">Guten Tag, '.$name.'';
    $items .= '<li style="padding: 1.37em 1em; font-family: inherit; color: #FFAD00;">'.$name2.'';

    }
    return $items;
    }
    ++++++++++++++++
    Screenshot here

    Please be so kind and gimme a little help :-)

    Cheers joerg

  7. joergwigger
    Member
    Posted 3 weeks ago #

    Fixt it :-9 Code is OK but Menu space was tooo small ...

Reply

You must log in to post.

About this Topic