How to Create Custom Links in BuddyPress

April 5th, 2009 Jeremy Winter BuddyPressTechnologyWeb Development 9 Comments

Recently I have been thinking of ways to add greater usability to my BuddyPress project ebodia.com. One thing I noticed is that when new users sign up, they are redirected to the homepage.

I felt this left new users with a “What now?” feeling, so I thought it would be a good idea to add a second navigation bar with some helpful links to the buddypress home theme. The Main roadblock I found was how to create custom links for users that were logged in. I did some digging and was able to find a snippet of code that will automatically parse the correct user id into the link.

How is this useful?

A few of the direct links I added for recently logged in users was -

  • A link to their Profile
  • A link to their list of blogs
  • A link to their BuddyPress account settings

I felt this would be useful for new users that weren’t sure where to start.

Code for Custom Links in BuddyPress

This code will work for most custom links you will need, but not all of them. For example, I was not able to create a link to a users blog directly, so instead I created a link to their list of blogs.

Here is the Old example code to add a link to the logged in users friend activity. The new version is after this one.

<a href="<?php global $bp; echo bp_core_get_userurl($bp->loggedin_user->id); ?>activity/my-friends">Your Friends Activity</a>

Update:
Andy P. suggested a alternative to the code above. Instead of declaring the bp global variable and requesting the logged in user id, why not just use a handy template tag? This code will output the same thing as the code above, but uses a very useful BuddyPress template tag.

<a href=”<?php echo bp_loggedin_user_domain() ?>activity/my-friends”>Your Friends Activity</a>

The above code will output this link

http://yousite.com/members/logged-in-user-id/activity/my-friends

If you take a look at the links in your own profile you should be able to see the small bit of code you will need to customize the links. Activity/myfriends can be replaced in the original code by profile/public to create a link to the logged in users profile, for example.

I want to thank Burtadsit for providing the original code used in the above example and Andy P. for providing the template tag AND BuddyPress.

Technorati Tags: buddypress examples, buddypress, buddypress code, custom links, links buddypress, buddypress profile, using buddypress, Building Link, construct this URL, variable user name, username, buddypress themes

From Film Camera to Canon Rebel Xti
New Elgg Theme: Custom Blue (Elgg 1.5)

Comment with Facebook

9 Comments


  1. Thank you for this wonderful code! Just what I needed…


  2. Also you can use the template tag:

    <a href=”<?php echo bp_loggedin_user_domain() ?>activity/my-friends”>Your Friends Activity</a>


  3. @ Andy – Thanks for the suggestion Andy. It makes more sense to use the template tag, instead of declaring the bp global variable and then requesting the logged in user id.

    I’ll update the post with the new alternative.


  4. I’d been trying to implement the code Andy suggested and I just couldn’t get it to work. After numerous trial and error, I found that this code works for linking to the logged-in users profile:

    <a href="<?php bloginfo(‘url’) ?>/members/<?php echo $user_identity ?>">My Profile</a>

    not sure if this more efficient, but it works for me


  5. @forlogos – I tried your code and was not able to get it too work correctly. I tested Andy’s version on my BP install and the link worked correctly. I am using BP 1.0.1 and WPMU 2.7.1,

    Perhaps we are using different versions?


  6. the code I used worked, but I discovered later that it would work only when the publicly displayed username matched the login username. So it really isn’t good.

    I did a slew of upgrades last night and now have BP 1.0.1 & WPMU 2.7.1 – not sure what versions I had beforehand – but Andy’s code works perfectly now.

    Most likely unrelated, but I was also having trouble with cookie integration with BBpress and WPMU/BP and things went smoothly after I went from version 0.9.0.5 to 1.0-RC-3


  7. Excellent, I am glad you got the links working. BuddyPress is moving along so quickly I find it hard to keep up as old code depreciates and new easier options become available. I’ll make a note in the post about making sure to upgrade before using the code example. Thanks for helping me narrow down what version the code is compatible with.

    I now see why Andy really wants us to upgrade before we post questions in the forum! Thankfully, BuddyPress has moved to a one-click upgrade process.


  8. That would be great if it did.

    Thanks for your help and for your post!


  9. Sorry to wade in so late in the day but can seen to get the coding to work.

    What I am trying to do is use a graphical button to link out to a user function…

    ie. Using a button called My Mail to link out to the buddypress Messages entry rather than using the native menu.

    Tried everything I can think of, the code link above resolves to a 404 error.

    any help would be greatly appreciated :)