Skip to:
Content
Pages
Categories
Search
Top
Bottom

Specify Menue – Add “edit profile”

  • @afu24

    Participant

    Hi,

    Is used the code snippet from the codex and it worked fine.

    How could I modify the code to choose a specific menu?

    E.g. primary oder secondary. Actually “Edit Profil” is shown in all my menues and I just want to have it on my Primary Menu.

    // Filter wp_nav_menu() to add profile link
    add_filter( 'wp_nav_menu_items', 'my_nav_menu_profile_link' );
    function my_nav_menu_profile_link($menu) {
        if (!is_user_logged_in())
            return $menu;
        else
            $current_user = wp_get_current_user();
            $user=$current_user->user_login ;
            $profilelink = '<li><a href="/forums/users/' . $user . '/edit">Edit Profile</a></li>';
            $menu = $menu . $profilelink;
            return $menu;
     
    }

    Layout and functionality – Examples you can use

    Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • @robkk

    Moderator

    you would need to do something like this.

    Since it is hard to guess what your menu’s theme location is , i just put primary.

    This does work in some default themes like Twentyfourteen though.

    add_filter('wp_nav_menu_items','rk_bbp_menu_profile_link', 10, 2);
    function rk_bbp_menu_profile_link( $items, $args ) {
        if( is_user_logged_in() && $args->theme_location == 'primary')  {
    		
    	$current_user = wp_get_current_user();
            $user = $current_user->user_login ;
    	$profilelink = '<a href="/forums/users/' . $user . '/edit">Edit Profile</a>';
            $items .=  '<li>' . $profilelink .  '</li>';
    
    	}
        return $items;
    }

    @afu24

    Participant

    @Robkk awesome, that is exactly what I was looking for. Thanks!!!

    Is there an option to set the position order of the menu item?
    Now its positioned after the normal menu items added at the theme design/menues.

    @robkk

    Moderator

    yeah that sounds about right that it should do that, and you probably could position it but i haven’t tried messing with that yet.

    where do you want the link exactly??

    @specstanza

    Participant

    Hi!

    To anyone in need of a login menu item with both user avatar and Link to the Bbpress dashboard in 2022, here’s my version of @robkk ‘s code :

    add_filter('wp_nav_menu_items','rk_bbp_menu_profile_link', 10, 2);
    function rk_bbp_menu_profile_link( $items, $args ) {
        if( is_user_logged_in() && $args->theme_location == 'menu-1')  {
    	$current_user = wp_get_current_user();   
    	    $user = $current_user->user_login ;
        $avatar = get_avatar( $current_user->ID, 64 );
    	$items .= '<li  class="NavBarLogin"><a href="/forums/users/' . $user . '/edit">' . $avatar .  'Mon compte</a></li>';
    }
    return $items;
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.
Skip to toolbar