[buddypress-dev] Getting Custom Field Values
Andy Peatling
andypeatling at automattic.com
Fri Jun 13 19:12:46 GMT 2008
On 11-Jun-08, at 5:23 AM, David J. Bisset wrote:
> Hi all,
>
> I wanted to share this function I built in bp-xprofile-
> templatetags.php which allows one to grab custom profile values.
>
> function get_field_value($fieldname, $output_field_name = false,
> $dropdown = false) {
> global $profile_template;
> foreach ($profile_template->groups as $group) {
> foreach ($group->fields as $field) {
> if ($field->name == $fieldname && $field->data->value != '') {
>
> if ($output_field_name) {
> return $field->name . ": " . $field->data->value;
> } else {
> return $field->data->value;
> }
> }
> }
> }
> }
This is great, but the trouble I see is that you are having to loop
through everything to find the value you want. That is expensive.
What I've done is created a function that will query the DB for the
specific values you want. This should be *a lot* faster.
I'm going to check the function into the trunk later today, it can be
used as follows:
bp_get_field_data( $field_names [, $user_id] );
$field_names = Either a string containing the name of the field, or an
array of field names.
$user_id = Optional. This will default to the id for the current user
being viewed, or you can pass an id for a specific user.
The function will return a string containing the field value if you
only pass one field. It will return an array of values with the field
name as the key if you pass an array of field names.
Examples:
<?php echo bp_get_field_data( 'First Name' ); ?>
<?php
$profile_data = bp_get_field_data( array( 'First Name', 'Last
Name', 'Home Town' ) );
echo 'First Name: ' . $profile_data['First Name'];
echo 'Last Name: ' . $profile_data['Last Name'];
echo 'Home Town: ' . $profile_data['Home Town'];
?>
That should hopefully satisfy the need to select and render specific
profile fields rather than just looping them all.
I may include this function in a 0.3.3 release of extended profiles.
> This works for single-value profile values such as textboxes and
> select dropdowns. Looking for ways to improve the function when it
> comes to multiple-value values which would look like this in the
> output (assuming the values "online" and "local" are selected):
>
> a:2:{i:0;s:6:"Online";i:1;s:5:"Local";}
unserialize() will do that.
Cheers
Andy
---------
Andy Peatling | Social Engineer | Automattic
http://andyinlife.com
More information about the buddypress-dev
mailing list