BuddyPress Friendships: An admin method to see all friendships in a concise manner?

Hi there!

I'm wondering if anyone has a method that I could obtain a report of our BuddyPress site friendships, displaying the username-to-username friendship. In the database, it is of course all indicated as numerical user IDs. We need to be able to see a list of the friendships. Database example attached. Maybe someone can help me write a query statement that will output to a usable spreadsheet?

Thanks for any help or insight!

James

  • Ash

    Hello @James Wolfensberger

    I hope you are well today.

    Please use the following code:

    <?php
    function show_bp_friendship_cb( $atts ){
    	$atts = shortcode_atts( array(
    		'hide_alone' => false
    	    ), $atts );
    
    	global $wpdb;
    	$users = get_users();
    	$friends = array();
    	foreach( $users as $user ){
    		$sql = "SELECT * from " . $wpdb->prefix . "bp_friends where initiator_user_id = '{$user->ID}' or friend_user_id = '{$user->ID}'";
    		$results = $wpdb->get_results( $sql );
    		$temp = array();
    
    		if( $atts['hide_alone'] && count( $results ) == 0 ) continue;
    
    		foreach( $results as $result ){
    			if( $result->initiator_user_id == $user->ID ){
    				array_push( $temp, $result->friend_user_id );
    			}else{
    				array_push( $temp, $result->initiator_user_id );
    			}
    		}
    
    		$friends[$user->ID] = $temp;
    	}
    
    	$html = '<table cellpadding="5" cellspacing="5" width="100%">';
    	$html .= '<tr>';
    	$html .= '<td valign="top" width="25%"><b>';
    	$html .= 'Name (Username)';
    	$html .= '</b></td>';
    	$html .= '<td valign="top"><b>';
    	$html .= 'Friends';
    	$html .= '</b></td>';
    	$html .= '<tr>';
    
    	foreach( $friends as $key => $val ){
    		$html .= '<tr>';
    			$html .= '<td>';
    				$user = new WP_User( $key );
    				$html .= '<a href="'. bp_core_get_user_domain( $key ) .'">' . $user->display_name . ' (' . $user->user_login . ')</a>';
    			$html .= '</td>';
    			$html .= '<td>';
    				$f = '';
    				foreach( $val as $v ){
    					$user = new WP_User( $v );
    					$f .= '<a href="'. bp_core_get_user_domain( $v ) .'">' . $user->display_name . ' (' . $user->user_login . ')</a>, ';
    				}
    				$html .= rtrim( $f, ", " );
    			$html .= '</td>';
    		$html .= '</tr>';
    	}
    	$html .= '</table>';
    	return $html;
    
    }
    add_shortcode( 'show_bp_friendship', 'show_bp_friendship_cb' );

    Use shortcode [show_bp_friendship] - by default it will show the users that don't have any friends.
    To hide those users use shortcode [show_bp_friendship hide_alone="true"]

    You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name 'mu-plugins'. If there is no folder in that name, then create a folder, name it 'mu-plugins', create a file inside that, give any name you like and paste the code in there. You don't need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

    You can see the code in gist too: https://gist.github.com/bappi-d-great/9b7ef63655424645e36e

    Hope it helps :slight_smile: Please feel free to ask more question if you have.

    Cheers
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.