How to display BuddyPress extended profile data on every page

BuddyPress is a software add-on that extends WordPress into an online community, which is great for creating your own social networks. I’m just finishing up a project at work where I’m using it to build out our new Intranet and it’s been fun working with it.

That said, I found that I was having trouble with displaying the extended field data that BuddyPress offers because its functionality was constrained to usage on pages of our site that was using their engine.

Normally, when wanting to display field data, you’d add the following PHP code to your template and Bob’s your uncle:


  <?php bp_profile_field_data('field=FIELDTITLE'); ?>


The problem is that it only displays the field data on profile pages and other pages that are specific to BuddyPress’ functionality, like forums, groups, and the like.

On the sidebar for my site, there’s a section similar to the profile widget on LinkedIn, where it shows your avatar, name, and job title. It’s the job title that wasn’t displaying everywhere when using the code above.

The solution…using xprofile_get_field_data to fetch the field data for the logged-in user.

Replace FIELDTITLE with your field name:


  <?php echo ' ' . xprofile_get_field_data( 'FIELDTITLE', bp_loggedin_user_id() ); ?>


Worked like a charm. Now the job title for this young intern now displays anytime he’s logged into the site, whether it’s in the header’s menu:

BuddyPress field display on all pages example

…or the left sidebar:

BuddyPress field display on all pages example 2

I gotta admit, after all these years, I still love this kind of work.

Have fun!

Post a comment

Fred looking at his camera phone with a dumb grin on his hairy face About

I'm an Ottawa-based web designer and front end developer who's been in the industry since HTML was written in all-caps with inline CSS. I like writing about the web as well as my other creative interests. Follow me on Twitter

Discussion

Requests for help are welcome but my schedule means I may be unable to respond right away.