Skip to:
Content
Pages
Categories
Search
Top
Bottom

How do I use Buddypress xprofile fields inside bbPress?

  • @evagorasc

    Participant

    I am mentioning @johnjamesjacoby who first started a thread about this some time ago, and @djpaul who asked that I start a new thread.

    So, I am running the latest version of BuddyPress and bbPress together (running bbPress as site-wide forums integrated with BuddyPress). I would like to add any custom xprofile BuddyPress fields that I created underneath the avatar of a user in the forums.

    The actual code that writes out the replies loop is inside the “loop-single-reply.php” file of the bbPress plugin. But how do I connect to BuddyPress and display any xprofile fields that I want though?

Viewing 18 replies - 1 through 18 (of 18 total)
  • @evagorasc

    Participant

    Made some progress with this, but still need help. I figured that I could simply add something like this underneath the avatar:

    Code:
    $location = xprofile_get_field_data( ‘Location’, 10 ) ;
    echo ‘Location: ‘ . $location;

    However, notice the hard-coded “10” there which is an ID. This needs to be the ID of each user in the replies table and it changes with each table row inside the “loop-single-reply.php” file. I don’t mind changing the template file and keeping a record for it when upgrades come, but how do I know the ID of each user in the bbPress posting loop?

    @modemlooper

    Moderator

    `$user_id = $bp->displayed_user->id;

    $location = xprofile_get_field_data( ‘Location’, $user_id ) ;
    echo ‘Location: ‘ . $location;

    if that doesnt work try getting the_author_ID(); or get_post_author_id();`

    @evagorasc

    Participant

    @modemlooper Thank you! That did it.
    get_the_author_id() as well as bbp_get_reply_author_id() seem to both work just fine.
    get_post_author_id() and $bp->displayed_user->id don’t seem to work for me though.

    @modemlooper

    Moderator

    When you are not in a BuddyPress loop you can get BP content by using WP author id as it returns same id.

    @evagorasc

    Participant

    @modemlooper thank you for your help.

    @evagorasc

    Participant

    Aha. Problems.

    So, following the advice of @modemlooper I got this working just fine. Anything created under Buddypress custom user fields can display fine in the bbPress loops, using something like this:

    `
    $user_location = xprofile_get_field_data( ‘Location’, bbp_get_reply_author_id() ) ;
    if (strlen($user_location) > 0 ) :
    echo ‘

    Location: ‘ . $user_location . ‘

    ‘;
    endif;
    `

    However, how do I make sure I follow the custom fields’ restrictions regarding visibility? In BuddyPress we can create a custom field and set its visibility to be either:
    1) Anyone
    2) Logged In Users
    3) My Friends

    Furthermore, these fields’ visibility can be allowed to be changed by each user.

    So, before simply displaying a custom field for just everyone, I need to run the required checks to verify that it should be displayed for the anonymous/logged-in user. I tried getting a reference to the actual field, like this but the var_dump() doesn’t help me much:

    `$user_location_field = xprofile_get_field( ‘Location’, bbp_get_reply_author_id() ) ;`

    This is the var_dump($user_location_field):

    `object(BP_XProfile_Field)[29]
    public ‘id’ => null
    public ‘group_id’ => null
    public ‘parent_id’ => null
    public ‘type’ => null
    public ‘name’ => null
    public ‘description’ => null
    public ‘is_required’ => null
    public ‘can_delete’ => null
    public ‘field_order’ => null
    public ‘option_order’ => null
    public ‘order_by’ => null
    public ‘is_default_option’ => null
    public ‘default_visibility’ => null
    public ‘allow_custom_visibility’ => string ‘allowed’ (length=7)
    public ‘data’ => null
    public ‘message’ => null
    public ‘message_type’ => string ‘err’ (length=3)`

    Even though I see a couple of public methods like “default_visibility()” and “allow_custom_visibility()”, they don’t really help. Their value doesn’t even change even when I change the field properties in the CMS.

    @modemlooper

    Moderator

    You have to pass id # of field a string wont work.

    `$data = xprofile_get_field( 2, bp_displayed_user_id() );

    print_r($data) ;`

    This will not give you a members choice. It will only return the default visibility you set up in admin. to get a users visibility on a field you get from user meta.

    `$user_visib = get_user_meta( bp_displayed_user_id(), bp_get_user_meta_key( ‘bp_xprofile_visibility_levels’ ), true );

    print_r($user_visib) ;`

    @evagorasc

    Participant

    @modemlooper thanks! I used a combination of your tips and info I found here https://bpdevel.wordpress.com/2012/03/16/profile-field-visibility-in-bp-1-6/ to make this work for me. I now have any custom field in Buddypress show up in my forums loop and it shows up conditionally as well for the appropriate users only.

    @modemlooper

    Moderator

    Can you can post a code snippet so others that search can find info on this?

    @evagorasc

    Participant

    Sure thing @modemlooper. The code below will look for all the custom fields that a user has filled in, compare them to what should be visible for the currently logged-in user and then loop and display the forum reply user’s data. Instead of simply looping the default titles and data of the xprofile fields, I do a custom switch/case so that I can format each field individually for more control.

    `
    $user_strAllFields = get_user_meta( bbp_get_reply_author_id(), bp_get_user_meta_key( ‘bp_xprofile_visibility_levels’ ), true );
    // get all the completed user fields for this poster
    if( $user_strAllFields != null ) :
    $user_arrAllFields = array_keys( $user_strAllFields );
    else :
    $user_arrAllFields = array();
    endif;
    // get all the fields that should not be displayed for this poster
    $user_arrHiddenFields = bp_xprofile_get_hidden_fields_for_user( bbp_get_reply_author_id() );

    // loop through all the poster filled-in fields
    foreach( $user_arrAllFields as $field ) :
    // exclude the ones that should not be displayed
    if( !in_array( $field, $user_arrHiddenFields ) ) :
    // turn the ID of a field into its object
    $obj_field = xprofile_get_field( $field, bbp_get_reply_author_id() ) ;

    // depending on the field name, customize the display
    switch( $obj_field->name ) :

    case ‘Location’ :
    $user_location = xprofile_get_field_data( ‘Location’, bbp_get_reply_author_id() ) ;
    if (strlen($user_location) > 0 ) :
    echo ‘

    Location: ‘ . $user_location . ‘

    ‘;
    endif;
    break;

    endswitch;
    endif; // !in_array( $field, $user_arrHiddenFields ) )
    endforeach; //( $user_arrAllFields as $field )
    `

    @iameverywhere

    Participant

    I don’t get where I add this code. I only need to add an xprofile field below the avatar on forum replies. I think this is exactly what I need but when I add this

    $user_location = xprofile_get_field_data( ‘Location’, bbp_get_reply_author_id() ) ;
    if (strlen($user_location) > 0 ) :
    echo ‘
    
    Location: ‘ . $user_location . ‘
    
    ‘;
    endif;

    below the <?php do_action( 'bbp_theme_before_reply_author_details' ); ?> it breaks my forums hard.

    Any idea how I am supposed to use that code to make it work?

    MANY THANKS!

    @shanebp

    Moderator

    Remove your changes.

    Untested, but try this in your theme/functions.php

    function bbp_add_xprofile_field() { 
      $user_location = xprofile_get_field_data( ‘Location’, bbp_get_reply_author_id() ) ;
      if (strlen($user_location) > 0 ) 
       echo 'Location: ' . $user_location;
    }
    add_action( 'bbp_theme_after_reply_author_details', 'bbp_add_xprofile_field' );

    @shanebp

    Moderator

    Make sure that all quotes are not curved, as in
    xprofile_get_field_data( ‘Location’
    in the post above.
    They should be straight, like the ones in the add_action in the post above.

    @iameverywhere

    Participant

    so to include it in my loop-single-reply.php i would add this code?

    <?php do_action( 'bbp_theme_after_reply_author_details', 'bbp_add_xprofile_field' ); ?>

    @iameverywhere

    Participant

    Dang doesn’t seem to work :c

    @shanebp

    Moderator

    You don’t have to add the do_action.
    It already exists in the template file.

    Just :
    – remove any changes you made to the template
    – add the function I provided to your theme/functions.php
    – make sure all quotes are single and straight, sometimes they are changed during copy / paste.

    I just did a test and it works fine.

    @iameverywhere

    Participant

    IT WORKS!

    @iameverywhere

    Participant

    Just a heads up, because I know a lot of people have asked and not gotten anywhere, but you can use this feature + Buddypress Xprofile Custom Fields Type plugin to create a forum badges feature. That was my goal with this and it works. You just need to display:none; the field# class so that users dont see it as an option to edit in their profile. Then you have complete control and can give users badges that show up in the forums 😀

    *flys away*

Viewing 18 replies - 1 through 18 (of 18 total)
  • The topic ‘How do I use Buddypress xprofile fields inside bbPress?’ is closed to new replies.
Skip to toolbar