Here an example which work when you’re in a members loop. You could use from within bp-custom. Note: it’s just a way to do it, may exist others.
function xyz( $user_id = 0, $field_name = 'Location' ) {
$return = false;
if ( empty( $user_id ) || empty( $field_name ) || ! bp_is_active( 'xprofile' ) ) {
return $return;
}
// the bp way to get user datas
$location = xprofile_get_field_data( 'Location', $user_id );
// output the div only if $location has data
if ( ! empty( $location ) ) {
echo '<div>'. $location .'</div>';
}
}
// we show this on the member header, using an pre-defined action hook in members-header.php
add_action( 'bp_before_member_header_meta', 'xyz' );
Superb, thank you @danbp!
Not only answered my question but this also gives me the fundamentals to implement further ideas I have for my BP site like displaying profile fields based on user roles.
Cheers 🙂
A quick follow up question…
Can I echo the xprofile_get_field_data fields elsewhere in WordPress (outside of Buddypress) in the same way?
ie :
$location = xprofile_get_field_data( 'Location', $user_id );
echo $location;
Obviously replacing user_id with the numeric user id.
One other note about the code you provided, It only works if I declare the user_id in the variable. ($user_id = 2), but that’s obviously not practical here. How do I set that to get the user_id of the current profile being viewed?
You just need an existing user_id.
He an example where you get a field value to add in a custom action in one of your template.
function bpfr_field( $custom_field ) {
global $bp;
// is xprofile component active ?
if ( bp_is_active( 'xprofile' ) )
// fetch the user_id
$current_user = wp_get_current_user();
$current_user_id = $current_user->ID;
// fetch the data
$custom_field = xprofile_get_field_data('42', $current_user_id); // 42 is a field ID
// show the data
echo '<div class="authorinfo">'. $custom_field . '</div>';
}
add_action( 'myfield', 'bpfr_field', 1 );
Then, add this at the appropriate place in your template <?php do_action( 'myfield'); ?>
Another example, which you can add at the begin of a custom function.
global $bp;
// Check if the Activity component is active before using it.
if ( ! bp_is_active( 'activity' ) ) {
return;
}
if( bp_is_user() && ! bp_get_member_user_id() ) {
$user_id = 'displayed: '. bp_displayed_user_id();
} else {
$user_id = 'get_member_user: '. bp_get_member_user_id();
}
// your stuf here
Note that both examples use global $bp, a check for active component with a different usage for a same result: if not active, the function will do nothing. This allows you to deactivate a component without throwing a warning or a php failure despite the function is in your theme, bp-cutom or plugin.
Brilliant that’s perfect, thanks again @danbp 😉
Hey @danbp,
Sorry bro, I can’t get it to return the user_id.
This is what i’ve got in bp-custom.php :
function field_location() {
global $bp;
// Check if the Activity component is active before using it.
if ( ! bp_is_active( 'activity' ) ) {
return;
}
if( bp_is_user() && ! bp_get_member_user_id() ) {
$user_id = 'displayed: '. bp_displayed_user_id();
} else {
$user_id = 'get_member_user: '. bp_get_member_user_id();
}
$field_name = 'Location';
$return = false;
if ( empty( $user_id ) || empty( $field_name ) || ! bp_is_active( 'xprofile' ) ) {
return $return;
}
// the bp way to get user datas
$location = xprofile_get_field_data( 'Location', $user_id );
// output the div only if $location has data
if ( ! empty( $location ) ) {
echo '<div style="margin-bottom:5px;">Location: '. $location .'</div>';
}
}
// we show this on the member header, using an pre-defined action hook in members-header.php
add_action( 'bp_before_member_header_meta', 'field_location' );
And then I’m calling it in member-header.php like this :
<?php field_location(); ?>
Using do_action( ‘field_location’ ); isn’t working for me but the above method does.
But it returns nothing. Only if I declare the user_id variable somewhere will it return the data for that user.
Doh.. Don’t worry I see what’s happening.. Need more coffee 😉