Support » Plugins and Hacks » Hacks » BuddyPress Profile Link

  • I want to create a “My Profile” link in one of my menus and I found the following code:

    // 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
                       $profilelink = '<li><a href="' . bp_loggedin_user_domain( '/' ) . '">' . __('My Profile') . '</a></li>';
                        $menu = $menu . $profilelink;
                        return $menu;
        }

    However it applies the My Profile link to ALL my menus. I only want it to apply to the menu I’ve registered as ‘usernav’

    Any ideas?? Thanks!

Viewing 6 replies - 1 through 6 (of 6 total)
  • @mercime

    (@mercime)

    Volunteer Moderator

    If usenav is what you find at ‘theme_location’ => ’usenav’ of that specific wp_nav_menu, then modify your code like so

    function mme_nav_menu_profile_link($menu, $args) {
    	$profilelink = '<a href="' . bp_loggedin_user_domain( '/' ) . '">' . __('My Profile', 'buddypress' ) . '</a>;
    	if (is_user_logged_in() && 'usenav' == $args->theme_location )
    		return $profilelink . $menu;
    	else
    		return $menu;
    }
    add_filter( 'wp_nav_menu_items', 'mme_nav_menu_profile_link', 10, 2);

    Hi @mercime
    OK, I tried this:

    // Filter wp_nav_menu() to add profile link
        add_filter( 'wp_nav_menu_items', 'my_nav_menu_profile_link' );
        function mme_nav_menu_profile_link($menu, $args) {
    	$profilelink = '<a href="' . bp_loggedin_user_domain( '/' ) . '">' . __('My Profile', 'buddypress' ) . '</a>;
    	if (is_user_logged_in() && 'usenav' == $args->theme_location )
    		return $profilelink . $menu;
    	else
    		return $menu;
    }
    add_filter( 'wp_nav_menu_items', 'mme_nav_menu_profile_link', 10, 2);

    but it gave me a white screen. My site is JoshuaTrent.com by the way. I have a few menus, top category, category and the menu in question which is “usernav” and only active when someone is logged in.

    @mercime

    (@mercime)

    Volunteer Moderator

    @jbt103 first thing. BuddyPress is not activated in your site, the code won’t work without BP. You also have BP Template Pack plugin activated and that’s not good without BP.

    Second thing, double-check theme location name in your theme’s header.php file. In the example at http://codex.wordpress.org/Function_Reference/wp_nav_menu#Adding_a_Word_at_the_Beginning_of_the_Menu
    ‘theme_location’ => ‘primary’
    and primary is what you’d be using in the line:
    if (is_user_logged_in() && 'primary' == $args->theme_location )

    I wrote that wrong – I used

    // Filter wp_nav_menu() to add profile link
        add_filter( 'wp_nav_menu_items', 'my_nav_menu_profile_link' );
        function mme_nav_menu_profile_link($menu, $args) {
    	$profilelink = '<a href="' . bp_loggedin_user_domain( '/' ) . '">' . __('My Profile', 'buddypress' ) . '</a>;
    	if (is_user_logged_in() && 'usernav' == $args->theme_location )
    		return $profilelink . $menu;
    	else
    		return $menu;
    }
    add_filter( 'wp_nav_menu_items', 'mme_nav_menu_profile_link', 10, 2);

    It’s still showing up white.
    I do have BuddyPress installed and working, although I limited some of the add on features like newsfeed and such. I checked my header and the theme location is ‘usernav’ so I’m still not sure what I’m doing wrong.

    @mercime

    (@mercime)

    Volunteer Moderator

    @jbt103 tested this one

    // Filter wp_nav_menu() to add profile link
    add_filter( 'wp_nav_menu_items', 'mme_nav_menu_profile_links', 10, 2 );
    function mme_nav_menu_profile_link($menu, $args) {
        if( is_user_logged_in() && $args->theme_location == 'usenav' ){
            $profilelink = '<li><a href="' . bp_loggedin_user_domain( '/' ) . '">' . __('My Profile') . '</a></li>';
            $menu = $profilelink . $menu;
        }
        return $menu;
    }

    Hi,

    Thank you. This is wat I was looking for. Do you have any idea if it is possible to use bp_get_loggedin_user_nav() to get the whole logged in bp user menu at once in the main menu? I tried this code:

    add_filter( 'wp_nav_menu_items', 'mme_nav_menu_profile_links', 10, 2 );
    function mme_nav_menu_profile_links($menu, $args) {
        if( is_user_logged_in() && $args->theme_location == 'usenav' ){
    
            $profilelink = '<li><a href="' . bp_loggedin_user_domain( '/' ) . '">' . __('My Profile') . '</a>
                                <ul class="sub-menu">' . bp_get_loggedin_user_nav() .  '</ul>
                            </li>';
    
            $menu = $profilelink . $menu;
        }
        return $menu;
    }

    but the menu is not styled proberly. Does anybody know how to do this?

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘BuddyPress Profile Link’ is closed to new replies.