Skip to:
Content
Pages
Categories
Search
Top
Bottom

add Welcome User to menu?

  • @ajiaim

    Participant

    I have a top menu that I want to display Welcome Username once the user logs in and ideally once they click it they get taken to their profile page on BBPress/BuddyPress.

    My original code was as below but it only showed log out once the user logs in.

    add_filter( 'wp_nav_menu_items', 'woohoo_add_auth_links', 10 , 2 );
    function woohoo_add_auth_links( $items, $args )
    {
    	if( $args->theme_location == 'topmenu' )
    	{
    		if ( is_user_logged_in() ) {
    			$items .= '<li><a href="'. wp_logout_url() .'">Log Out</a></li>';
    		}
    		elseif ( !is_user_logged_in() ) {
    			$items .= '<li><a href="'. site_url('wp-login.php') .'">Log In</a></li>';
    			$items .= '<li><a href="'. site_url('wp-login.php?action=register') .'">Register</a></li>';
    		}
    	}
    	return $items;
    }

    I then changed it to the below but it doesn’t link or align up nicely…

    add_filter( 'wp_nav_menu_items', 'woohoo_add_auth_links', 10 , 2 );
    function woohoo_add_auth_links( $items, $args )
    {
    if( $args->theme_location == 'topmenu' )
    {
        if ( is_user_logged_in() ) {
        $current_user = wp_get_current_user();
        printf( 'Welcome %s!', esc_html( $current_user->user_firstname ) );
        }
        elseif ( !is_user_logged_in() ) {
            $items .= '<li><a href="'. site_url('wp-login.php') .'">Log In</a></li>';
            $items .= '<li><a href="'. site_url('wp-login.php?action=register') .'">Register</a></li>';
        }
    }
    return $items;
    }

    Any ideas?

Viewing 7 replies - 1 through 7 (of 7 total)
  • @robin-w

    Moderator

    without knowing what that looks like – I suspect you won’t get a response

    Can you post a link to a screenshot of what the issue is?

    @ajiaim

    Participant

    Sorry, this is what I’m trying to modify
    http://prntscr.com/cg901p

    @robin-w

    Moderator

    So are you trying to get it to say logout, or are you trying to get it to say ‘welcome’…

    @ajiaim

    Participant

    It’s currently saying Log out but I’m trying to say Welcome *username* which also links to their profile.

    @robin-w

    Moderator

    ok, without having time to do all the work, I’ve quickly edited this bit of code – it may need debugging, but should get you there with some playing

    add_filter( 'wp_nav_menu_items', 'rew_edit_profile', 10,2 );
    
    function rew_edit_profile ($menu, $args) { 
    global $bsp_login ;		
    if (!is_user_logged_in())
    		return $menu;
    	//if primary set and not primary then return
    	if ($args->theme_location !== 'primary' )   {
    		return $menu ;
    	}
    	
    	else
    		$current_user = wp_get_current_user();
    		$user=$current_user->user_nicename  ;
    		$user_slug =  get_option( '_bbp_user_slug' ) ;
    			if (get_option( '_bbp_include_root' ) == true  ) {	
    			$forum_slug = get_option( '_bbp_root_slug' ) ;
    			$slug = $forum_slug.'/'.$user_slug.'/' ;
    			}
    			else {
    			$slug=$user_slug . '/' ;
    			}
    			//set link to welcome xxx
    			$edit_profile= 'Welcome!'. esc_html( $current_user->user_firstname ) ;
    						
    			//get url
    			$url = get_site_url(); 
    			$profilelink = '<li> <a href="'. $url .'/' .$slug. $user . '/edit">'.$edit_profile.'</a></li>';
    			
    
    			
    		$menu = $menu . $profilelink;
    		return apply_filters( 'rew_edit_profile', $menu );
    }

    @ajiaim

    Participant

    Amazing!!!! Thank you so much!!!!

    @robin-w

    Moderator

    no problem !

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.
Skip to toolbar