How to add user profile link to specific menu?
-
Hi,
I have two menu areas on my website: main and top. I would like to add user profile link to top menu only.
I found this example, which was were useful: https://codex.bbpress.org/layout-and-functionality-examples-you-can-use/#8-add-edit-profile-to-a-wordpress-menu But it adds link to both menus, top and main. I would like to add user profile link only to top menu, without adding it to main menu.
Can you help me please?
-
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; }
Where can I view additional menu arguments of a specific menu?
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?
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.
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; }
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;
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.
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.
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; }
Unfortunately, it didn’t help
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; }
It helped. Thank you very much!
no problem 🙂
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
- You must be logged in to reply to this topic.