Using Custom Profile Field Data in Buddypress Templates

November 18th, 2009 by Peter Anselmo Leave a reply »

I ran into what I thought would be fairly common need for Buddypress theme development, the ability to pull data from a custom user profile field and display it. After searching a bit, I could only find way to pull either random fields, or all of the fields together. So after digging around in the core files, I found the pieces to put together my own function for the task.

1. Create the file “bp-custom.php” and place it directly in the plugins folder. Note, do not place it in the “buddypress” folder, otherwise it will be overwritten when upgrading. If the file exists, simply append to it rather than creating a new one.

2. Paste the following function in:

function bp_the_site_member_profile_data($field_name = '', $echo = 1) {
  if( !$field_name ) {
    return false;
  }
  global $site_members_template;
  if( function_exists( 'xprofile_get_field_data' ) ) {
    $data = xprofile_get_field_data( $field_name,
                                     $site_members_template->member->id);
    if( $echo ) {
      echo "$data";
      return true;
    } else {
      return $data;
    }
  }
}

3. You can now use the function ‘bp_the_site_member_profile_data(‘MyField’) in your templates (of course, substituting ‘MyField’ with the one you’d like to use). Note, this must be place inside a ‘Members Loop’ so that the $site_members_template variable is populated.  Odds are, you’re inserting this near other profile data already in the loop, so there should be no problems.

UPDATE:
As of BuddyPress 1.2, this functionality has been included. While in the members loop, you can use the included function: ‘bp_member_profile_data( ‘field=the field name’ )’ to much the same effect.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

2 comments

  1. Eric says:

    Very interesting – do you see the ability to add profile updates to the activity stream using this method – I’ve been wanting this feature for a while now.

  2. Lindsay says:

    Hey.. Thanks for publishing this little snippet. I have been trying to create a way to display the members list and sort them by a specific field value.

    For instance, when a new user signs up they have to check off one of the following:

    Type of work:
    Photographer
    Painter
    Professional Services
    Fitness

    The goal would be to list each member under their work-type heading.. Grouping them I guess.

    I can do it manually.. and create 15 loops for each type, but then if I add a new type I have to go in and build ANOTHER loop.

    I figured that I should simply be able to get all values of a field using xprofile_get_field_data and pass that as a variable and simply do a “foreach” loop… Basically foreach $value as ‘value’

    I am not the strongest with php, but really didn’t think this would be as difficult as it is!! I can’t even get past the ability to simply get all of the field values!

    If you are interested in helping, it would be greatly appreciated.

Leave a Reply