Skip to content

Instantly share code, notes, and snippets.

@bappi-d-great
Last active August 29, 2015 14:14
Show Gist options
  • Save bappi-d-great/9b7ef63655424645e36e to your computer and use it in GitHub Desktop.
Save bappi-d-great/9b7ef63655424645e36e to your computer and use it in GitHub Desktop.
List of BuddyPress friends of all users
<?php
// 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"]
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' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment