Helping you Build Your Own Social Network!

Faster, better and easier!

Fetching and Showing only Specific fields from Buddypress XProfile data

Jotted by Brajesh Singh in Buddypress, Buddypress Tricks on October 28, 2009
We are Hiring new WordPress/buddyPress developers

so you want to fetch only specific fields from buddypress profile and want to show it somewhere (may be in the sidebar or on members directory page).

here is a solution, you can either fetch the profile fields by passing name or by passing the field id.

let us see the code, say you have a field Location and you want to show it on the member directory page.

So, here is what we do.


<?php

//code inside the directory loop

$location= xprofile_get_field_data( "Location" ,bp_get_member_user_id());//fetch the text for location

?>

Please note,the user id field, the second argument is optional. If you are on the profile pages and you don’t want to provide user id, It will be taken as the displayed user’s id

So, On profile page,  you can write something like this


<?php

$location= xprofile_get_field_data( "Location");//fetch the location field for the displayed user

?>

On other pages, It is the bestg practice to pass the user_id of the user whose details you want to fetch.

Handling Check box, Multi select boxes

When multiple check box are allowed or multiple options are allowed to be selected, buddypress stores the data in serialized form. We may use the following code to unserialize it and echo it

 echo echo bp_unserialize_profile_field( xprofile_get_field_data(‘CheckboxFieldName’, $user_id_whom_data_you_want));

Hope it helps. I used while creating a custom theme which had sidebars displaying a few fields from profile.

and yeh, if you used it or other alternatives, do let me know your views :)

PS: This post was imported from my other blog,so code went missing.Thanks to mercime for pointing the issue . Now the code is there :)

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • BlinkList
  • DZone
  • FriendFeed
  • IndianPad
  • MySpace
  • Netvouz
  • Reddit
  • SphereIt
  • StumbleUpon
  • Technorati

Related posts:

  1. Introducing BuddyPress Facebook Connect+: Advance Facebook login/registration Plugin for your BuddyPress based Social Network
  2. Creating The sitewide global/unified search Page for your Buddypress Theme
  3. Buddypress trick:Redirect Users to their profile on login
  4. Using activity as wire in Buddypress 1.2 themes
  5. Buddypress Recent Profile Visitors Plugin

21 Responses to Fetching and Showing only Specific fields from Buddypress XProfile data

  • Hi, code is missing in this post. Please share the “trick” Brajesh.
    Thanks

    • hi mercime
      Thanks for the comment. I have updated the page now. I missed it while importing the post from my other blog.
      Hope it helps.

      Thanks
      Brajesh

  • Hi Brajesh,

    Thanks for this – it is very helpful. I’m having a bit of a problem getting it working in the members directory though. It works fine on the members own page but I can’t seem to get it working in the directory. I’ve put it in members-loop.php.

    Are you sure your code for calling the user_id is correct? Given that it works on the members page that is the only thing that I can guess would be going wrong.

    Thanks for you help,
    Andrew.

    • hi Andrew
      Thanks for the comment. It should not work with bp 1.2 on members directory.
      Try to use

      bp_get_member_user_id()

      In place of bp_get_the_site_member_user_id() and It should work in members directory.

  • Thanks a million for that Brajesh – sorted!

  • Hi Brajesh
    I just tried out your tutorial but am having some problems – it just doesn’t seem to appear on my members directory page. I’m using buddypress 1.2.5.2 (latest version) and wordpress 3.0.1(latest version).
    What i’ve done so far: I’ve gone to ‘Profile Field Setup’ on the WordPress Dashboard under the Link ‘Buddypress’ and added the fields ‘Business Type’ with a drop down box of three options – DJs, Venue or Promoter. So when users register they choose one of these business types. I wanted these to show up on the members directory so put in to members-loop.php this code:

    Any idea where i might be going wrong. Many thanks in advance!
    Dorothy

  • Hi Brajesh,

    is it possible to get this work with checkboxes (multiselect)?

    I am using latest BP 1.2.9. with WP 3.2

    Thanks,
    Michael

    • Hi Micheal,
      I am sorry, I missed your post completely.
      here is what you need.

      echo bp_unserialize_profile_field( xprofile_get_field_data(‘CheckboxFieldName’, $user_id_whom_data_you_want));

      I hope it helps some of the people looking for it.

  • @Brajesh Singh, thanks for this code, please how can i add a checking function to this code? am trying to use it on profile page so that if the field is not filled by the member it will just show let say a link “add your Location” and when click it will just go to the place where he can fill the field.

  • Any help on the code Brajesh? i have uploaded the code am using

    regards

  • @sbrajesh, did you get the code i uploaded? i still need solution for this, you can help play around with the code when you less busy. lol

    regards

    • Hi Abbey,
      just got the time today. Appreciate your patience.
      here is the solution you are looking for
      https://gist.github.com/1193494

      Once you copy these two function(you can make changes in the ‘show_or_ask_to_fill’ function), you can call it for a field like show_or_ask_to_fill(‘Company’) or so on.

      Hope that helps.

  • @sbrajesh, thanks so much for the code, i really appreciate your effort, i dont know what am doing wrong, i copy the code and change the show_or_ask_to_fill field to (‘Country’) and put all the code in member-header.php, but nothing happened,it just show all the code as text on my site.

  • @sbrajesh, I had no luck with this code, i have put the php opening and closing tag, but the code keep crashing my member-header. am using bp 1.2.9 and wp 3.2. with child theme. thanks so much brajesh anyways. you are a star and we all appreciate your effort.

    Regards

    • Hi Abbey,
      That is strange. The code is running fine for me. Try putting the code in functions.php/bp-custom.php and then check if there is any error. If yes, It might be due to whitespace/ending begining code. If there is no error, then use the ‘show_or_ask_to_fill’ function with field name and make sure you use php closing/begining tags here too.

  • Brajesh and the others ! I KNOW WHAT THE ERROR IS !!!!

    For X-Profil Field works very very fine, no problem – but i try to use the field from the first folder ( is not x-profile) and i search the google since 3 hours nothing really help me,

    Brjaesh – what should i use instead THIS : $dob_time=xprofile_get_field_data($dob_field_name, $user_id);

    Please Help me (us)

  • Hey!! Thanks a million, been looking for more info on these functions. Im wondering how i would go about adding tags around the data pulled from the custom profiles. Im adding categories to the members directory using the above functions – how would i go about adding an tag around each of the categories returned [more than one category is returned sometimes so i cant put it around the entire function]. The profile does this already, im currently looking through the code to see how its done but would love a hand if you know!

    Cheers,
    Jonny!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Get Adobe Flash playerPlugin by wpburn.com wordpress themes