calling specific profile fields individually
-
Hey,
is there a way to call specific profile fields within the profile loop instead of only calling groups of profile fields?
I am creating a new template and want to position each profile field separately. I only managed to do this with whole profile-field-groups by putting the group id into the loop. it didn’t work with field-ids though.
thanks in advance
christoph
Running Buddypress 1.1 and the latest wordpress mu
-
echo xprofile_get_field_data( $field_name_or_id [, optional $user_id] );
How do I echo the field’s data?
The line “echo xprofile_get_field_data( $field_name_or_id [, optional $user_id] );” only displays “echo xprofile_get_field_data( $field_name_or_id [, optional $user_id] );”
@wordpressfan did you wrap <? ?> around it?
@andy that still doesn’t seem to work. <?php echo xprofile_get_field_data($2); ?> gives an error. <?php echo xprofile_get_field_data($name); ?> too.
<?php echo xprofile_get_field_data('2'); ?>
would work, where 2 is the id of the field.
The next step would be to add some code checking first to see whether there is any data for that field. If true, print the data, if not echo ‘(no data found)’.
Thanks for the quick help. It works fine with me.
Any quick idea, how to link it to the members search like usual profile fields do?
greetings from berlin
Christoph
Doesn’t work for me. Field id ‘2’ is supposed to be company name in my installation, but this in the members_loop.php only displays name and last active:
<h3><a href="<?php bp_the_site_member_link() ?>"><?php bp_the_site_member_name() ?></a></h3>
<p><?php do_action( 'bp_core_directory_members_content' ) ?></p>
<p><?php echo xprofile_get_field_data('2'); ?></p>
<label><?php bp_the_site_member_last_active() ?></label>Should I use bp_the_site_member_something(‘2’)?
I recommend using the field name, just because it’s easier to keep track of…
Example…
<?php if ( $firstname = xprofile_get_field_data('First Name') ) : ?>
<h3><?php _e('First Name', 'your-theme-textdomain') ?></h3>
<div class="profile-data-field">
<?php echo $firstname ?>
</div>
<?php endif; ?>…will get you the data from the field named “First Name” for the currently displayed user. If there is no user displayed (your account or anyone else’s), it will return false.
Thanks for the additional information. I’ve tried this in members-loop:
<?php if ( $company = xprofile_get_field_data('Company') ) : ?>
<p><?php echo $company ?></p>
<?php endif; ?>and this
<p><?php echo xprofile_get_field_data('Company'); ?></p>
Still get nothing with either version…
Within the members loop, the problem is that you’re not feeding it any user_id to get the data for… Like I said above, without a user_id, it’s just looking for the $bp->displayed_user->id, which doesn’t exist because when you’re on a directory page, there is no displaued_user->id; you’re not looking at a member…
Within any member related loop, you will need to make a custom function for that loop and use the user_id available to you to spit out what you need…
Put this in your bp-custom.php… (haven’t tested this, wrote freehand, lookout for bugs)
function bp_custom_member_list_xprofile_data( $field ) {
echo bp_custom_get_member_list_xprofile_data( $field );
}
function bp_custom_get_member_list_xprofile_data( $field ) {
global $site_members_template;
return xprofile_get_field_data( $field, $site_members_template->member->id );
}Then use it like this on fields that need checking…
<?php if ( $company = bp_custom_get_member_list_xprofile_data('Company') ) : ?>
<p><?php echo $company ?></p>
<?php endif; ?>…or like this on fields that don’t…
<p><?php bp_custom_member_list_xprofile_data('Company') ?></p>
Excellent, that works very nicely. Thanks John James Jacoby!
Could something like this become part of the core? When you have the name of a member, it should be easy to add associated data. This function makes it a lot easier.
Edit: The function probably needs to be expanded a bit. I get these slashes (escapes?) in the output:
Pandora's Box
I’ll look into that when I have time…
This works great on single field entries, but for checkboxes it outputs stuff like
a:3:{i:0;s:19:”Beschaffung/Einkauf”;i:1;s:26:”Marketing & -kommunikation”;i:2;s:20:”Strategie/Innovation”;}
where as in the sidebar (random member) it comes through just fine.
How can I fix this?
thanks
the random profile data function does some magic like this:
function bp_the_site_member_random_profile_data() {
1138 global $site_members_template;
1139
1140 if ( function_exists( 'xprofile_get_random_profile_data' ) ) { ?>
1141 <?php $random_data = xprofile_get_random_profile_data( $site_members_template->member->id, true ); ?>
1142 <?php echo wp_filter_kses( $random_data[0]->name ) ?>
1143 <?php echo wp_filter_kses( $random_data[0]->value ) ?>
1144 <?php }
1145 }
How can I apply that to James’ code above to fix the output?
or maybe something with unserialize&stripslashes?
I found this in reference to the code mentioned above:
* xprofile_format_profile_field()<br />
955 *<br />
956 * Formats a profile field according to its type. [ TODO: Should really be moved to filters ]<br />
957 *<br />
958 * @package BuddyPress Core<br />
959 * @param $field_type The type of field: datebox, selectbox, textbox etc<br />
960 * @param $field_value The actual value<br />
961 * @uses bp_format_time() Formats a time value based on the WordPress date format setting<br />
962 * @return $field_value The formatted value<br />
963 */<br />
964 function xprofile_format_profile_field( $field_type, $field_value ) {<br />
965 if ( !isset($field_value) || empty( $field_value ) )<br />
966 return false;<br />
967<br />
968 $field_value = bp_unserialize_profile_field( $field_value );<br />
969<br />
970 if ( 'datebox' == $field_type ) {<br />
971 $field_value = bp_format_time( $field_value, true );<br />
972 } else {<br />
973 $content = $field_value;<br />
974 $content = apply_filters('the_content', $content);<br />
975 $field_value = str_replace(']]>', ']]>', $content);<br />
976 }<br />
977<br />
978 return stripslashes( stripslashes( $field_value ) );<br />
979 }and this
function bp_unserialize_profile_field( $value ) {
522 if ( is_serialized($value) ) {
523 $field_value = maybe_unserialize($value);
524 $field_value = implode( ', ', $field_value );
525 return $field_value;
526 }
527
528 return $value;
529 }
I wanted to do the same thing as h4x3d and tried his dirty trick which works..
Anyhow here is how I did it (dunno if this is less dirty)
$platforms = xprofile_get_field_data('Plattformen');
$data = xprofile_format_profile_field('checkbox', $platforms);
echo $data;rfauster has the right idea.
This works great! But I am wondering if there is a way to have it call the value depending on the user that is currently logged-in.
I thought something like this might work… <?php echo bp_loggedinuser_xprofile_get_field_data(’10’); ?> but no such luck.
Any ideas?
Hi , I have the same problem with checkboxes like in the post on the first page , I get weird stuff , I am using on member-header.php this simple code :
`Birth:`
for normal entry it works but datepicker data and checkboxes data are giving me nonsenses . Can anybody help me to find a code to get normal value from checkboxes ? dirty code above I don’t see anymore – old post – thanks !
- The topic ‘calling specific profile fields individually’ is closed to new replies.