Skip to:
Content
Pages
Categories
Search
Top
Bottom

Display user country flag


  • davehakkens
    Participant

    @davehakkens

    Hello.

    I would like to show country information from users in our forums next to their name.
    The country information is taken from the profile fields.

    Option 1
    Is to show the country name directly from the profile field data.
    I’ve almost got this done by putting this snippet in the loop-single-reply.php
    <div id="country"> <?php echo bp_member_profile_data('field=country'); ?></div>
    However this displays the country of the user that started the topic, not who made the reply.

    Option 2
    This would be a bit more difficult but preferred. And that is showing a country flag. flag

    I’m stuck on how to continue. Other topics about this are super outdated and don’t provide an answer. Any suggestions?

Viewing 11 replies - 1 through 11 (of 11 total)
  • 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.


    davehakkens
    Participant

    @davehakkens

    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.


    danbp
    Moderator

    @danbp

    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' );

    davehakkens
    Participant

    @davehakkens

    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..


    danbp
    Moderator

    @danbp

    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


    davehakkens
    Participant

    @davehakkens

    Also doesn’t seem to work from the bpcustom.php 🙁

    Thanks for the link, never tried groups. Seems powerful though..


    danbp
    Moderator

    @danbp

    @davehakkens,

    have you uncommented the second filter ? The first one works with groups. Comment it and try the other.


    davehakkens
    Participant

    @davehakkens

    Still doesn’t work.
    Here is an example of the page where it should be displayed. Am I missing something?


    danbp
    Moderator

    @danbp

    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.


    davehakkens
    Participant

    @davehakkens

    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>

Viewing 11 replies - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.
Skip to toolbar