Hi @davehakkens
bp_member_profile_data() defaults to the current user in the members loop… It is probably coincidence this is the thread starter user, but I can’t remember how bbPress interacts with it.
What I think you want to do is
bp_get_member_profile_data(array('field'=>'country', 'user_id' => bbp_get_reply_author_id()))
That way you’re passing the bbPress reply author ID to the BP function, to fetch the data for that user. Hope that helps.
thanks for the help @djpaul.
<div id="country"> <?php echo bp_get_member_profile_data(array('field'=>'country', 'user_id' => bbp_get_reply_author_id())); ?></div>
However this code results in showing the location of the user itself, me.
Not the user that made the reply.
Well, debug what bbp_get_reply_author_id
is, etc. It looks like it should work, but I don’t know where exactly you’re adding this code (if it’s outside the reply templating loop, it won’t work, I’m guessing) — and this is something you should just debug locally until you figure it out.
hi @davehakkens,
You use a standalone bbPress. Normally you have to ask on bbPress forum for such questions.
Add this to bp-custom.php or child theme functions.php and give a try.
i tested it with group forums only, so i couldn’t test all the filters.
You might to find/experiment others.
It’s a working example who fires the country value. But you’ll certainly have to complete it to get the flag image.
function country_flag() {
$user_id = bbp_get_reply_author_id();
$country = xprofile_get_field_data( 'country', $user_id );
echo '<div class="country">'. $country .'</div>';
}
add_filter( 'bbp_theme_after_reply_author_details', 'country_flag' );
//add_filter( 'bbp_theme_before_topic_author', 'country_flag' );
What do you mean with standalone version @danbp? I use a bbpress and buddypress plugin. I figured this is more for Buddypress since it has to do with buddpress profile fields.
Anyway thanks for the code, looks good! But it doesn’t seem to work after adding it into the functions.php. I haven’t got experience using filters so can’t seem to find the problem..
If it doesn’t work in your child theme functions.php, use bp-custom.php
Standalone because bbP is used separetely and not as group forum, which is a popular config when BuddyPress is activated. FYI (in case of):
Installing Group and Sitewide Forums
Also doesn’t seem to work from the bpcustom.php 🙁
Thanks for the link, never tried groups. Seems powerful though..
@davehakkens,
have you uncommented the second filter ? The first one works with groups. Comment it and try the other.
Still doesn’t work.
Here is an example of the page where it should be displayed. Am I missing something?
The single topic view template (loop-single-reply.php
) has 2 actions hooks you can use:
do_action( 'bbp_theme_before_reply_author_details' );
do_action( 'bbp_theme_after_reply_author_details' );
In so far, the above function should work.
Try this one. Add it into your theme function to test. And if it works from there, remove it and add it to your child-theme functions.php file.
function country_flag() {
if( class_exists( 'bbPress' ) ) :
$user_id = bbp_get_reply_author_id();
$country = xprofile_get_field_data( 'country', $user_id );
echo '<div class="country">'. $country .'</div>';
endif;
}
add_filter( 'bbp_theme_after_reply_author_details', 'country_flag' );
Activate also wp_debug in wp-config.php while testing. It could be possible you have an error somewhere who avoid the function to work properly…
If it still doesn’t work, ask for help on the bbpress support.
OK, we got it to work in a different way. Special thanks to @coffeywebdev for the help.
For those interested:
This snippet is used in the functions.php to get the locations from the profile fields and show the flag image.
function dh_get_flag_by_location($country){
if($country <> '' && !empty($country)){
$country_filename = get_stylesheet_directory_uri() . '/img/flags/' . sanitize_file_name($country) . '.png';
$country_path = get_stylesheet_directory() . '/img/flags/' . sanitize_file_name($country). '.png';
if(file_exists($country_path)){
$html = '<img src="' . $country_filename . '"/>';
} else {
$html = $country;
echo '<!--' . get_stylesheet_directory_uri() . '/img/flags/' . sanitize_file_name($country) . '-->';
}
echo $html;
}
}
Then we needed to change 2 templates to show this flag image in
the Bbpress forum replies and on the buddypress members page. So we added the code necessary to show the flag
bbpress/loop-single-reply.php
<div id="country" style="float:left;margin-right:.5em;"> <?php $user = get_userdata( bbp_get_reply_author_id() ); $country = xprofile_get_field_data( 42, $user->ID ); dh_get_flag_by_location($country); ?></div>
buddypress/members/members-loop.php
<div class="member-location"> <?php $country = bp_get_member_profile_data('field=Location'); dh_get_flag_by_location($country); ?></div>