Skip to:
Content
Pages
Categories
Search
Top
Bottom

Adding profile link to Menu


  • daileycon
    Participant

    @daileycon

    I’m using the following code to add an edit profile link in the menu. But it adds it to All menus, I have a top, main and footer menu. I just want it added to the top menu. Any help?

    // 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 , $args) {
        if (is_user_logged_in() && $args->theme_location == 'primary')
            return $menu;
        else
            $current_user = wp_get_current_user();
            $user=$current_user->user_nicename ;
            $profilelink = '<li><a href="/forums/users/' . $user . '/edit">Edit Profile</a></li>';
            $menu = $menu . $profilelink;
            return $menu;
      
    }
Viewing 7 replies - 1 through 7 (of 7 total)

  • Robin W
    Moderator

    @robin-w

    probably this should work

    // 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 , $args) {
        if (is_user_logged_in() && $args->theme_location == 'primary') {
            $current_user = wp_get_current_user();
            $user=$current_user->user_nicename ;
            $profilelink = '<li><a href="/forums/users/' . $user . '/edit">Edit Profile</a></li>';
            $menu = $menu . $profilelink;
        }
    	return $menu;
      
    }

    daileycon
    Participant

    @daileycon

    That code gives me this error in all 3 menus. I just want Edit Profile in the top menu.

    Warning: Missing argument 2 for my_nav_menu_profile_link(), called in /home/wp_5fnr6w/my site/wp-includes/class-wp-hook.php on line 300 and defined in /home/wp_5fnr6w/my site/wp-content/themes/unicon/functions.php on line 749

    If I remove “&& $args->theme_location == ‘primary'” and ” , $args”

    The error goes away but I have Edit Profile in all three menus.


    daileycon
    Participant

    @daileycon

    Maybe the problem is “primary” is not one of my 3 menus? How do I get the menu slugs?


    daileycon
    Participant

    @daileycon

    Ok I found my menu slugs in my functions file.

    // Add Custom Primary Navigation
    function minti_register_custom_menu() {
    	register_nav_menu('main_navigation', 'Main Navigation');
    	register_nav_menu('footer_navigation', 'Footer Navigation');
    	register_nav_menu('topbar_navigation', 'Topbar Navigation');
    }

    but I still get that error when changing primary to topbar_navigation

    // 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 , $args) {
        if (is_user_logged_in() && $args->theme_location == 'topbar_navigation')
            return $menu;
        else
            $current_user = wp_get_current_user();
            $user=$current_user->user_nicename ;
            $profilelink = '<li><a href="/forums/users/' . $user . '/edit">Edit Profile</a></li>';
            $menu = $menu . $profilelink;
            return $menu;
    }

    daileycon
    Participant

    @daileycon

    I DID IT!!!! Now this adds an edit profile link in my top bar menu only if someone is logged in! Whooo hooo

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

    Robin W
    Moderator

    @robin-w

    great – sorry I missed the 10,2 in my original post !!


    tylertervooren
    Participant

    @tylertervooren

    Glad you got it figured out. I just created this very simple plugin to print the profile url via shortcode in case you find it useful

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