Skip to:
Content
Pages
Categories
Search
Top
Bottom

Check "If friends" to display data conditionally

  • @leiascofield

    Participant

    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)
  • @leiascofield

    Participant

    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.

    @leiascofield

    Participant

    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() ) { ?>

    @leiascofield

    Participant

    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.

    @coldmanager

    Member

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

    @ankurm

    Member

    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

    @opencode

    Participant

    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”;
    }
    `

    @opencode

    Participant

    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";
    }`

    @funmi-omoba

    Participant

    @chouf1

    Participant

    @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)
  • The topic ‘Check "If friends" to display data conditionally’ is closed to new replies.
Skip to toolbar