Skip to:
Content
Pages
Categories
Search
Top
Bottom

Check "If friends" to display data conditionally

  • Avatar of Leia Scofield
    Leia Scofield
    Participant

    @leiascofield

    I would like to surround some profile content bits with an if/else statement to show only if the logged in user is a friend of the profile they are viewing. What is the best way to say “If user is a friend”?

    Rough example:

    <? if friend { ?>

    show content

    <? } else { ?>

    show nothing

    <? } ?>

Viewing 9 replies - 1 through 9 (of 9 total)
  • Avatar of Leia Scofield
    Leia Scofield
    Participant

    @leiascofield

    Not sure if this is the perfect way to do it, but here’s how I achieved what I was trying to do. This is in one of my templates:

    <?php

    global $bp, $friends_template;

    $friend_status = friends_check_friendship_status( $bp->loggedin_user->id, $bp->displayed_user->id );

    if ( $friend_status == ‘is_friend’ || $bp->loggedin_user->id == $bp->displayed_user->id ) {

    ?>

    This checks to see if the user you’re looking at is a friend OR you, and then would display the information following it. You would then follow up with an else that shows the information non-friends see.

    Avatar of Leia Scofield
    Leia Scofield
    Participant

    @leiascofield

    Even better:

    <?php

    global $bp, $friends_template;

    $friend_status = friends_check_friendship_status( $bp->loggedin_user->id, $bp->displayed_user->id );

    if ( $friend_status == ‘is_friend’ || bp_is_my_profile() ) { ?>

    Avatar of Leia Scofield
    Leia Scofield
    Participant

    @leiascofield

    Even better!

    Add this to your functions.php:

    function bp_is_friend() {

    global $bp;

    if ( ‘is_friend’ == BP_Friends_Friendship::check_is_friend( $bp->loggedin_user->id, $bp->displayed_user->id ) )

    return true;

    return false;

    }

    Now you can drop bp_is_friend() in wherever you want to check if the displayed user is a friend of the logged in user, such as:

    <?php if ( bp_is_friend() || bp_is_my_profile() ) { ?>

    The above line checks if the displayed user is a friend or is the user.

    Avatar of ColdManager
    ColdManager
    Member

    @coldmanager

    hi, well ,it not work for me , is there another way?

    This Would work….

    `global $bp;
    $loggedin_user_id = $bp->loggedin_user->userdata->ID;
    $other_user_id = bp_get_member_user_id();
    if($loggedin_user_id == $other_user_id){
    $check_string = ‘its_me’ ;
    }
    else{
    $check_string = friends_check_friendship_status( $loggedin_user_id,$other_user_id);
    }
    if ( $check_string == ‘is_friend’ ) {
    /*Your Code Here*/
    }
    if ( $check_string == ‘not_friends’ ) {
    /*Your Code Here*/
    }
    if ( $check_string == ‘pending’ ) {
    /*Your Code Here*/
    }
    if ( $check_string == ‘its_me’ ) {
    /*Your Code Here*/
    }
    ?>`

    You can also use this
    $other_user_id = $bp->displayed_user->id ;
    for the other user id

    Avatar of OpenCode
    OpenCode
    Participant

    @opencode

    Here an update

    `global $bp;

    $loggedin_user_id = bp_loggedin_user_id();
    $other_user_id = bp_displayed_user_id();

    if($loggedin_user_id == $other_user_id){
    $check_string = ‘its_me’ ;
    }
    else{
    $check_string = friends_check_friendship_status( $loggedin_user_id,$other_user_id);
    }

    if ( $check_string == ‘is_friend’ ) {
    //Your Code Here
    echo “Is Friend”;
    }
    if ( $check_string == ‘not_friends’ ) {
    //Your Code Here
    echo “Not Friends”;
    }
    if ( $check_string == ‘pending’ ) {
    //Your Code Here
    echo “Pending”;
    }
    if ( $check_string == ‘its_me’ ) {
    //Your Code Here
    echo “Its You”;
    }
    `

    Avatar of OpenCode
    OpenCode
    Participant

    @opencode

    If you are trying to have this within your child theme or somewhere like in authors.php below then you might want to have a small change.

    `
    global $bp;

    $loggedin_user_id = bp_loggedin_user_id();
    $other_user_id = $curauth->ID; // <<< a change to get the id of author

    if($loggedin_user_id == $other_user_id){
    $check_string = 'its_me' ;
    }
    else{
    $check_string = friends_check_friendship_status( $loggedin_user_id,$other_user_id);
    }

    if ( $check_string == 'is_friend' ) {
    //Your Code Here
    echo "Is Friend";
    }
    if ( $check_string == 'not_friends' ) {
    //Your Code Here
    echo "Not Friends";
    }
    if ( $check_string == 'pending' ) {
    //Your Code Here
    echo "Pending";
    }
    if ( $check_string == 'its_me' ) {
    //Your Code Here
    echo "Its You";
    }`

    Avatar of funmi omoba
    funmi omoba
    Participant

    @funmi-omoba

    Avatar of danbpfr
    danbpfr
    Participant

    @chouf1

    @funmi-omoba,

    please open a new tread, this one is 3 years old, and give some details about your config (version, theme name) and what you want to do.

    @opencode, thank you for sharing, but updating such old posts hasn’t much effect for most users. Also give some indication about BP version where your code will work on.

Viewing 9 replies - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.