Skip to:
Content
Pages
Categories
Search
Top
Bottom

How to add user profile link to specific menu?

Viewing 14 replies - 1 through 14 (of 14 total)

  • Robkk
    Moderator

    @robkk

    There is a way to put it into a specific menu if you know exactly what the theme location or any additional menu arguments of a specific menu are.

    Here is an example if the theme location is primary.

    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_nicename;
    	$profilelink = '<a href="/forums/users/' . $user . '/">Your Profile</a>';
            $items .=  '<li>' . $profilelink .  '</li>';
    
    	}
        return $items;
    }

    dayan89
    Participant

    @dayan89

    Where can I view additional menu arguments of a specific menu?


    dayan89
    Participant

    @dayan89

    I’ve learned the code of theme locations tab and found the name of my top menu. It was “top-menu”

    Now I have profile link in top menu. But how can I make it first item in menu, not last?


    Robkk
    Moderator

    @robkk

    You find this in your theme, most likely in the functions.php file, but it could be different depending on the theme. You would spot by the register nav menu function like what is listed in this page.

    https://codex.wordpress.org/Function_Reference/register_nav_menus

    If you have a free theme I could check it out and get the arguments you need.

    If not there may be some arguments like menu slug that could help, but I need the name of the menu you are wanting to put the profile link in.


    Robkk
    Moderator

    @robkk

    Try this.

    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 == 'top-menu')  {
    		
    	$current_user = wp_get_current_user();
            $user = $current_user->user_nicename;
    	$profilelink = '<a href="/forums/users/' . $user . '/">Your Profile</a>';
            $items .=  '<li>' . $profilelink .  '</li>';
    
    	}
        return $items;
    }

    dayan89
    Participant

    @dayan89

    I didn’t get. It’s same code you gave me above.

    Can you please modify following code. This worked out for me before, but it added two links, to primary and to top-menu.

    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_nicename ;
            $profilelink = '<li><a href="/forum/profile/' . $user . '">Мой профиль</a></li>';
            $menu = $profilelink . $menu;
            return $menu;
     

    Robkk
    Moderator

    @robkk

    The code I just gave you is not the same. I changed the theme location from primary to top menu. Did you try it??

    If you link me to your site and use the code you just posted, I can probably make it where the profile link only shows on the top menu using CSS.


    dayan89
    Participant

    @dayan89

    Yes, thank you for help, I got the link on top menu. 🙂 The question is not actual anymore. Sorry if I was not clear. If you take a look at my post above, you’ll find:

    Now I have profile link in top menu. But how can I make it first item in menu, not last?

    So now question is: how to make profile link – first item in top menu? Because now it’s shown as last item.


    Robkk
    Moderator

    @robkk

    I am sure the profile menu link has a class in it that you can find in the source code using the dev tools, and from there you can just float it to the left with CSS.

    something like

    .class {
      float: left;
    }

    dayan89
    Participant

    @dayan89

    Unfortunately, it didn’t help


    Robkk
    Moderator

    @robkk

    If it is only those two items, you could be able to float the other item to the right and if the unordered list is floated to the left it will make the profile link the first item.

    li#menu-item-2679 {
        float: right;
    }

    dayan89
    Participant

    @dayan89

    It helped. Thank you very much!


    Robkk
    Moderator

    @robkk

    no problem 🙂


    sfmPascal
    Participant

    @sfmpascal

    Hi, here is what I did for my site:

    1. Installed Allow PHP Execute
    2. Create a page, for example “forum profile”
    3. Add this code:

    
    <?php
    if (!is_user_logged_in()) {
      header('Location: /my-account/');
    } else {
      $current_user = wp_get_current_user();
      $user=$current_user->user_nicename;
      $newURL = '/forums/users/' . $user;
      header('Location: '.$newURL);
    }
    ?>
    

    4. Now when you go to this page, which you now can add to any menu location supported by your theme, if not logged in, will redirect to your login page, otherwise redirect to their form profile link.

    Jarrod

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