add Welcome User to menu?
-
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?
-
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?
Sorry, this is what I’m trying to modify
http://prntscr.com/cg901pSo are you trying to get it to say logout, or are you trying to get it to say ‘welcome’…
It’s currently saying Log out but I’m trying to say Welcome *username* which also links to their profile.
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 ); }
Amazing!!!! Thank you so much!!!!
no problem !
- You must be logged in to reply to this topic.