Fetching and Showing only Specific fields from BuddyPress XProfile data
Do 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 xprofile field id.
Before we see the code, let us assume that you have a field named Location and you want to show it on the member directory page.
Well, here is what we do.
1 2 3 | //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
1 | $location= xprofile_get_field_data( 'Location');//fetch the location field for the displayed user |
On other pages, It is the best 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
1 | echo bp_unserialize_profile_field( xprofile_get_field_data( 'CheckboxFieldName', $user_id_whom_data_you_want ) ); |
The above code does not work with BuddyPress 1.6+.
For BuddyPress 1.6+, The easiest way is to use the following code
1 2 3 | $data = bp_get_member_profile_data( array( 'field'=>'CheckBox Or Select Field Name','user_id' => $user_id ) );// use bp_get_members_user_id() or just remove that argument if you are inside the members loop(e.g. on members directory). echo $data; |
The above code works for normal field as well as multiple select/check boxes all.
Incase you wish to programitacally manipulate the output of the muliselecte/checkbox fields. You can do it like this
1 2 3 | $data = xprofile_get_field_data( $field, $user_id , $format = 'array' );//format is used to prepare the data when the field is multiple select/checkbox. //you can specify the $format to 'comma' for comma separated |
That will allow you to access the data as an array or comma separated values.
Here is an example, where we limit the number of visible selection to 2, on members directory for Education field.
1 2 3 4 5 6 7 8 9 10 | $data = xprofile_get_field_data( 'Course', bp_get_member_user_id() ); if ( $data && is_array( $data ) ) { $data = array_slice( $data, 0, 2 ); $data = join( ',',$data ); } echo $data; |
I am hoping that it helps you. I used it while creating a custom theme which had sidebars displaying a few fields from profile.
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 🙂
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 Dorothy,
did you try this update
https://buddydev.com/buddypress/fetching-and-showing-only-specific-fields-from-buddypress-xprofile-data/comment-page-1/#comment-3439
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.
Hi Abbey,
sorry your comment got into the queue and I could not reply. Do you still need a solution for this?
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.
Hi Abbey,
you will need to put the beginning and the ending php tags before and after the code.
@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)
PLEASE FORGET WHAT I SAID – it´s working now !!!!! FINE . PERFECT !
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!
Hi Brajesh!
I have a private website where I require people to enter their birth date for confirmation but I don't want to show that info on the user profile page. I've managed to hide that but I also want to be able to quickly see the info as an admin to be able to verify and activate the user. Is it possible to add Xprofile feeds to the users screen in WP-admin? Thanks!
Hi Magnus,
Thank you for the comment.
yes you can display the data on User edit page in the admin.
here is the sample code. You can either put it in bp-custom.php or the functions.php of you active theme.
https://gist.github.com/2839490
Hope that helps.
regards
Brajesh
Hi again!
Wow, thank you so much! That worked instantly and was really helpfull in my process of approving new users. Thanks!
Hello @sbrajesh,
I know you're busy but I thought I'd ask anyway. I want to do the reverse of what you've communicated here. So, I have a form (FormidablePro) that contains a variety of fields: text, checkbox, radio, and dropdown. I want to map those fields back to the XProfile fields (I will create those myself). So, when the form is submitted, the data in the form fields gets pushed to the appropriate XProfile fields and can be displayed accordingly. Apparently this can be done. All that is needed is to match up the meta names? At least, that is what I have been told. Still, I have no idea how to go about doing this.
I am asking for two reasons: first, I want to control access to the XProfile updates (only members who have access to the form will be able to update their info.); and second, I'll have finer control of the data that shows up in the Members profile–that is, a dedicated form plugin, such as FormidablePro can better direct what portions of the form are completed, thus a better user experience.
Can you point me in the right direction? Thanks!
Hi Quint,
Thank you for asking.
In fact, you can do that easily.
you can use this function
[sourcecode language="php"]
xprofile_set_field_data( $field, $user_id, $value);
[/sourcecode]
to update the field data, where the $field will be the Name of the Profile Field and $user_id is the id of the user whose profile you want to update.
Hope that helps.
Thanks @sbrajesh! And $value is the field ID of the field on the form?
Hi Quint,
I am not too familiar with the FormidablePro form, but here is something that may help you.
http://formidablepro.com/knowledgebase/action-hooks/frm_after_create_entry/
You can use the $_POST['The field id of formidable form'] as $value
You can find the field id of the formidable form in the admin.
Hi brajesh
I have a members directory which only displays 4 out of 13 profile fields. One of the profile fields is a multi select and I want to limit the number of options displayed on the memory directory so that it does not display more than three?
Many thanks.
Hi Mark,
I have updated the post with new code snippet to handle your need.
Can you please check again and let me know if that helps?
Regards
Brajesh
Hi Brajesh and thanx for your very usefull tricks !
I would like to display the name of the field facing each only filled field (in the members-loop)
e.g. My text : bp_member_profile_data( 'field=the field name' );
Thanx in advance for your attention !
Nicolas (bp 1.6x)
Hi Nicolas,
do you want to show individual profile fields or all profile fields for which data is set.
for individual profile, you can use something like this
[sourcecode language=""]
$data=bp_get_member_profile_data(array('field'=>'FieldName' Name','user_id'=>bp_get_members_user_id() );
if(!empty($data)){
echo "Field Name:".$data;
}
[/sourcecode]
or a variation of it for tables.
Thanx you very much for your time and attention Brasesh !
I will test this code asap
My goal was to show in every profil tab.
This will help me for sure.
Thanx you again.
This would make a great plugin! Something along the lines of "BuddyPress Custom Directory". The plugin would allow an admin to add any BP xProfile field to the directory and chose the horizontal position it is displayed. The directory could also be sorted by the fields, including text and date fields. Plus, allowing for setting the default Order By: would be nice as well.
Looking forward to it Brajesh!
~eric
Hi Eric,
That's an awesome suggestion 🙂 There are hooks present to make it possible. I am not sure about the sorting stuff though. Last time I had checked, buddypress was not able to sort via xprofile fields. I will certainly give it a try in a week or two 🙂
Thank You
Brajesh
You are welcome. I look forward to seeing it!
~eric
You're awesome. Thanks so much!
Hi is there a way to echo the cover image outside the loop on a sidebar.. I have searched everywhere but cannot find anything to do this (but I know its possible because I have seen other themes do it).. please help thank you.
Hi James,
Just posting here for reference. solution posted here if anyone else needs it
https://buddydev.com/support/forums/topic/echo-buddypress-profile-header-contents-in-sidebar/